Mega Code Archive

 
Categories / Delphi / Algorithm Math
 

How to sort a Stringlist with the CustomSort Method

Title: How to sort a Stringlist with the CustomSort Method function CompareDates(List: TStringList; Index1, Index2: Integer): Integer; var d1, d2: TDateTime; begin d1 := StrToDate(List[Index1]); d2 := StrToDate(List[Index2]); if d1 then Result := -1 else if d1 d2 then Result := 1 else Result := 0; end; procedure TForm1.Button1Click(Sender: TObject); var sl: TStringList; begin sl := TStringList.Create; try // listbox1.Sorted := False ! sl.Assign(listbox1.Items); sl.CustomSort(CompareDates); listbox1.Items.Assign(sl); finally sl.Free end; end; end. {********************************************************************} { To sort Integer values:} function CompareInt(List: TStringList; Index1, Index2: Integer): Integer; var d1, d2: Integer; r1, r2: Boolean; function IsInt(AString : string; var AInteger : Integer): Boolean; var Code: Integer; begin Val(AString, AInteger, Code); Result := (Code = 0); end; begin r1 := IsInt(List[Index1], d1); r2 := IsInt(List[Index2], d2); Result := ord(r1 or r2); if Result 0 then begin if d1 then Result := -1 else if d1 d2 then Result := 1 else Result := 0; end else Result := lstrcmp(PChar(List[Index1]), PChar(List[Index2])); end; procedure TForm1.Button1Click(Sender: TObject); var sl: TStringList; begin sl := TStringList.Create; try // listbox1.Sorted := False; sl.Assign(listbox1.Items); sl.CustomSort(CompareInt); listbox1.Items.Assign(sl); finally sl.Free; end; end;