uses System.IOUtils, System.Types, System.Generics.Collections, System.Generics.Defaults, System.Character;

procedure StrArrNumberSort(var Arr: TArray<string>);
begin
  TArray.Sort<string>(Arr,
    TComparer<string>.Construct(
      function (const s1,s2: string): Integer
      var
        L1,L2: string;
      begin
        L1 := TPath.GetFileNameWithoutExtension(s1);
        L2 := TPath.GetFileNameWithoutExtension(s2);
        if (IsDigit(L1[1]) and IsDigit(L2[1])) then
          Result := StrToIntDef(L1, 0) - StrToIntDef(L2, 0)
        else
          Result := CompareText(L1, L2);
        {考虑嵌套目录}
        L1 := TPath.GetDirectoryName(s1);
        L2 := TPath.getDirectoryName(s2);
        Result := CompareText(L1, L2) * MaxWord + Result;
      end
    )
  );
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  arr: TArray<string>;
  s: string;
begin
  arr := TArray<string>(TDirectory.GetFiles('c:\temp'));
  StrArrNumberSort(arr);
  for s in arr do Memo1.Lines.Add(s);
end;

posted on 2012-02-10 15:57  万一  阅读(7341)  评论(1编辑  收藏  举报