首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在Windows资源管理器中获得Delphi中的排序顺序?

如何在Windows资源管理器中获得Delphi中的排序顺序?
EN

Stack Overflow用户
提问于 2011-02-28 01:32:45
回答 2查看 4K关注 0票数 18

总结:

  1. 我一直在寻找的术语似乎是“自然排序”。
  2. for behaviors in operating systems:

代码语言:javascript
复制
- For Windows (version >= XP), Windows Explorer utilizes natural sort.
- For Linux terminals: use "ls -v" instead of plain "ls" to get natural sort.

  1. 在Delphi语言中编程,使用StrCmpLogicalW Windows API进行自然排序。
  2. 用于在Delphi & Kylix和Lazarus中编程,使用手工函数获得自然排序:
    • (1) Martin Pool的Delphi wrapper for natural Order字符串比较。

字母排序算法的http://irsoft.de/web/strnatcmp-and-natsort-for-delphi

  • (2)代码,用其他语言编写,来自davekeolle站点。

http://www.davekoelle.com/alphanum.html

  • (3)其他知识页面:

http://www.codinghorror.com/blog/2007/12/sorting-for-humans-natural-sort-order.html

http://objectmix.com/delphi/722211-natural-sorting-optimizing-working-solution.html

http://groups.google.com/group/borland.public.delphi.language.delphi.general/browse_thread/thread/1141d49f8bbba577

http://objectmix.com/delphi/401713-alphanumeric-sort-routine-delphi.html

==========================

以下文件名将在Windows资源管理器中排序,如下所示:

test_1_test.txt

test_2_test.txt

test_11_test.txt

test_12_test.txt

test_21_test.txt

test_22_test.txt

例如,如果我将它们放在一个TStringList实例中并调用Sort,则排序顺序如下:

test_1_test.txt

test_11_test.txt

test_12_test.txt

test_2_test.txt

test_21_test.txt

test_22_test.txt

以上文件名将在Cygwin的rxvt终端或CentOS等Linux发行版的xterm终端中进行排序,如下所示:

test_11_test.txt

test_12_test.txt

test_1_test.txt

test_21_test.txt

test_22_test.txt

test_2_test.txt

你能帮助评论一下如何理解这种排序行为的差异吗?此外,是否可以获得与Windows资源管理器中相同的顺序?欢迎提出任何建议!

附言:我的Windows区域设置为中文,但我认为英语区域设置也是如此。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-02-28 01:48:49

StrCmpLogicalW能够处理数字,另一种选择是CompareString

票数 21
EN

Stack Overflow用户

发布于 2011-02-28 02:15:34

感谢Anders -答案是StrCmpLogicalW;我在Delphi2009源代码中没有找到它的声明,所以我在下面的测试中自己声明了它:

代码语言:javascript
复制
type
  TMyStringList = class(TStringList)
  protected
    function CompareStrings(const S1, S2: string): Integer; override;
  end;

function StrCmpLogicalW(P1, P2: PWideChar): Integer;  stdcall; external 'Shlwapi.dll';

function TMyStringList.CompareStrings(const S1, S2: string): Integer;
begin
  Result:= StrCmpLogicalW(PChar(S1), PChar(S2));
end;

procedure TForm11.Button2Click(Sender: TObject);
var
  SL: TMyStringList;

begin
  SL:= TMyStringList.Create;
  try
    SL.Add('test_1_test.txt');
    SL.Add('test_11_test.txt');
    SL.Add('test_12_test.txt');
    SL.Add('test_2_test.txt');
    SL.Add('test_21_test.txt');
    SL.Add('test_22_test.txt');
    SL.Sort;
    Memo1.Lines:= SL;
  finally
    SL.Free;
  end;
end;
票数 18
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5134712

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档