How to get the sort order in Delphi as in Windows Explorer?

会有一股神秘感。 提交于 2019-12-28 03:02:06

问题


Summarization:

  1. The terminology that I have been looking for seems to be "natural sort".
  2. For behaviors in operating systems:

    • For Windows (version >= XP), Windows Explorer utilizes natural sort.
    • For Linux terminals: use "ls -v" instead of plain "ls" to get natural sort.
  3. For programing in Delphi, use StrCmpLogicalW Windows API to get natural sort.

  4. For programing in Delphi & Kylix & Lazarus, use hand-crafted functions to get natural sort:
    • (1) Delphi wrapper for Natural Order String Comparison by Martin Pool.
      http://irsoft.de/web/strnatcmp-and-natsort-for-delphi
    • (2) Codes of alphanum sorting algorithm in other languages from davekeolle site.
      http://www.davekoelle.com/alphanum.html
    • (3) Other knowledgable pages:
      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

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

The following file names will be ordered in the Windows Explorer as shown below:

test_1_test.txt

test_2_test.txt

test_11_test.txt

test_12_test.txt

test_21_test.txt

test_22_test.txt

If, for example, I put them in a TStringList instance and call Sort, the sorted order is as below:

test_1_test.txt

test_11_test.txt

test_12_test.txt

test_2_test.txt

test_21_test.txt

test_22_test.txt

And for record, the above file names will be ordered in the rxvt terminal of Cygwin or xterm terminal of Linux distributions such as CentOS as shown below:

test_11_test.txt

test_12_test.txt

test_1_test.txt

test_21_test.txt

test_22_test.txt

test_2_test.txt

Could you help to comment on how to understand this difference of sorting behaviors? Furthermore, is it possible to get the same order as in Windows Explorer? Any suggestion is appreciated!

PS: My Windows locale is set to Chinese but I would think the same for English locale.


回答1:


StrCmpLogicalW is able to handle numbers, the other alternative is CompareString




回答2:


Thanks to Anders - the answer is StrCmpLogicalW; I have not found it's declaration in Delphi 2009 sources, so I declared it myself in the test below:

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;


来源:https://stackoverflow.com/questions/5134712/how-to-get-the-sort-order-in-delphi-as-in-windows-explorer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!