delphi 自定义排序

本文探讨了在Delphi中如何实现自定义排序的详细步骤,通过实例展示了如何利用TList或TStringList的Sort方法,结合自定义比较函数来对数据进行复杂逻辑的排序。内容包括自定义比较函数的编写、排序稳定性以及优化技巧。
摘要由CSDN通过智能技术生成
在Delphi中自定义排序方法来排序字符串列表(custom sort method in Delphi to sort list of strings)
 Delphi	IT屋 2017/4/23 21:57:01 
百度翻译此文   有道翻译此文
问 题
I am trying to sort a list of files (they are stored as list of strings) in Delphi whose names look like below

a_1.xml
a_20.xml
a_10.xml
a_2.XML
when i use quick sort to sort the file names, it sorts the file names as below

a_1.xml
a_10.xml
a_2.xml
a_20.XML
But, I want the file names to be sorted in the below fashion

a_1.xml
a_2.xml
a_10.xml
a_20.XML
Any help will be greatly appreciated.

解决方案
You can use the same compare function that Explorer uses, namely StrCmpLogicalW.

function StrCmpLogicalW(psz1, psz2: PWideChar): Integer; stdcall;
  external 'shlwapi.dll';

function StrCmpLogical(const s1, s2: string): Integer;
begin
  Result := StrCmpLogicalW(PChar(s1), PChar(s2));
end;
If you have your strings in a TStringList instance then you can use
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值