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

前端 未结 2 1101
一个人的身影
一个人的身影 2020-11-30 11:02

Summarization:

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

    • For Win
相关标签:
2条回答
  • 2020-11-30 11:38

    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;
    
    0 讨论(0)
  • 2020-11-30 11:39

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

    0 讨论(0)
提交回复
热议问题