利用TStringList的CustomSort方法排序,下面是一个例子:
//降序排列
function DescCompareFunc(List: TStringList; Index1, Index2: Integer): Integer;
begin
; Result := -CompareText(List.Strings[Index1],List.Strings[Index2]);
end;
//升序排列
function AscCompareFunc(List: TStringList; Index1, Index2: Integer): Integer;
begin
; Result := CompareText(List.Strings[Index1],List.Strings[Index2]);
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
; FSList := TStringList.Create;
; FSList.Add('a');
; FSList.Add('c');
; FSList.Add('b');
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
; listbox1.Items.Assign(FSList);
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
; FSList.CustomSort(DescCompareFunc);
end;
procedure TForm1.Button4Click(Sender: TObject);
begin
; FSList.CustomSort(AscCompareFunc);
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
; FSList.Free;
end;
end.