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 给变量加引号

Delphi 如何将字符串中的半角字符转换为全角字符

Delphi 获取某个页面所有的链接

monthoftheyear:取得一个tdatetime变量的月份在年度中的索引

processid, process handle, window handle 之间的互相转换

Delphi windows 编程[5] - 学习窗体生成的过程五

Delphi多线程学习:多线程数据库查询(ado)

Delphi 文件夹拷贝子文件及文件夹

Delphi real控件 rm格式的播放器

Delphi 动态注册卸载ocx

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



打赏

取消

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

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

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

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

评论

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