发布网友 发布时间:2022-05-17 05:02
共2个回答
热心网友 时间:2022-05-17 06:31
function MyCustomCompare(List: TStringList; Index1, Index2: Integer): Integer;
var
S1, S2: string;
N1, N2: Integer;
begin
S1 := List[index1];
S2 := List[index2];
Result := 0;
if S1 = S2 then
Exit;
N1 := Pos('-', S1); //---
if N1 = 0 then
begin
Result := -1;
Exit;
end;
N2 := Pos('-', S2); //---
if N2 = 0 then
begin
Result := 1;
Exit;
end;
N1 := StrToIntDef(Copy(S1, 1, N1 - 1), 0);
N2 := StrToIntDef(Copy(S2, 1, N2 - 1), 0);
if N1 = N2 then
Result := 0
else
if N1 > N2 then
Result := 1
else
if N1 < N2 then
Result := -1;
end;
procere TForm1.Button1Click(Sender: TObject);
var
sl: TStringList;
begin
sl := TStringList.Create;
try
sl.Assign(lst1.Items);
sl.CustomSort(MyCustomCompare);
lst2.Clear;
lst2.Items.AddStrings(sl);
finally
sl.Free;
end; // try
end;
来个效果图:
热心网友 时间:2022-05-17 07:49
不用自己写代码排序,直接用它的Sorted属性就行了,比如: