Delphi TStringList排序功能应用
作者: 葛屹肃 | 日期: 2021-12-05 | 分类: 个人杂谈
今天做标签打印时,按用户要求,标签数据要按序号进行排序。之前是使用TStringList类进行处理的,于是搜索关于排序的功能,发现非常方便,只要几行代码即可实现。
方式一:
procedure TfrmInLabel.btnTestClick(Sender: TObject);
var
list: TStringList;
begin
list := TStringlist.Create;
try
list.CommaText := ’09,02,06′;
list.Sort;
Memo1.Text := list.CommaText;
finally
list.Free;
end;
end;
执行结果:’02,06,09′
方式二:
procedure TfrmInLabel.btnTestClick(Sender: TObject);
var
list: TStringList;
begin
list := TStringlist.Create;
try
list.CommaText := ’09,02,06′;
list.Sorted := true;
Memo1.Text := list.CommaText;
finally
list.Free;
end;
end;
执行结果:’02,06,09′
方式三(倒序):
function DescCompareStrings(List: TStringList; Index1, Index2: Integer): Integer;
begin
Result := -AnsiCompareText(List[Index1], List[Index2]);
end;procedure TfrmInLabel.btnTestClick(Sender: TObject);
var
list: TStringList;
begin
list := TStringlist.Create;
try
list.CommaText := ’09,02,06′;
List.CustomSort(DescCompareStrings);
Memo1.Text := list.CommaText;
finally
list.Free;
end;
end;
执行结果:’09,06,02′
总结,Delphi TStringList的排序是类自带功能,只需要设置一下Sort或Sorted即可,而要实现倒序,即要加入自定义函数DescCompareStrings。
文章链接:https://www.geyisu.com/2288.html