Delphi 字符串、文件路径 排序算法


本文整理自网络,侵删。

 


//正序
function SortItem(List: TStringList; Index1, Index2: Integer): Integer;
var A1, A2: String;
    Dir1, Dir2: String;
    Name1, Name2: String;
begin//delphitop.com
  A1 := List[Index1];
  A2 := List[Index2];

  Dir1 := ExtractFilePath(A1);
  Dir2 := ExtractFilePath(A2);

  Name1 := ExtractFileName(A1);
  Name2 := ExtractFileName(A2);

  if Dir1 = Dir2 then
    Result := AnsiCompareText(Name1, Name2)
  else
    Result := AnsiCompareText(Dir1, Dir2);
end;

//倒序
function SortItem2(List: TStringList; Index1, Index2: Integer): Integer;
var A1, A2: String;
    Dir1, Dir2: String;
    Name1, Name2: String;
begin
  A1 := List[Index1];
  A2 := List[Index2];
  //delphitop.com
  Dir1 := ExtractFilePath(A1);
  Dir2 := ExtractFilePath(A2);

  Name1 := ExtractFileName(A1);
  Name2 := ExtractFileName(A2);

  if Dir1 = Dir2 then
    Result := AnsiCompareText(Name2,Name1 )
  else
    Result := AnsiCompareText(Dir2, Dir1);
end;


procedure TForm1.Button1Click(Sender: TObject);
var
FList: TStringList;
begin
FList:=TStringList.Create;
FList.Text:=Memo1.Text;
FList.CustomSort(SortItem);
Memo1.Text:=FList.Text;
end;

相关阅读 >>

Delphi 自动关闭弹出的窗口

wmi cim_datafile的33个属性

Delphi 的 webservice 的 cookie 操作

Delphi tadodataset 加载数据到fdmemtable1

Delphi分离汉字和英文字母

Delphi 拼接文件路径

Delphi驱动开发研究第一篇--实现原理

Delphi vclskin 5.40在2010安装方法

d10环境下调用hidcontroller1.0.35获取数据乱码问题

Delphi 三层架构简单例子(经测试成功)

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...