在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
delphi 自定义排序
最新推荐文章于 2023-03-08 11:48:02 发布
本文探讨了在Delphi中如何实现自定义排序的详细步骤,通过实例展示了如何利用TList或TStringList的Sort方法,结合自定义比较函数来对数据进行复杂逻辑的排序。内容包括自定义比较函数的编写、排序稳定性以及优化技巧。
摘要由CSDN通过智能技术生成