Einzelnen Beitrag anzeigen

Benutzerbild von toms
toms
(CodeLib-Manager)

Registriert seit: 10. Jun 2002
4.648 Beiträge
 
Delphi XE Professional
 
#9

Re: Listbox nach Datum sortieren

  Alt 4. Jul 2009, 17:35
Bei mir funktioniert's so:

Delphi-Quellcode:
procedure ZufallsDatumHinzufuegen;
var
  i: Word;
begin
  for i := 0 to 1000 do
    Form1.Listbox1.Items.Add(DateToStr(EncodeDate(Random(109) + 1900, Random(12) + 1, Random(28) + 1)));
end;

function CompareDates(List: TStringList; Index1, Index2: Integer): Integer;
var
  d1, d2: TDateTime;
begin
  d1 := StrToDate(List[Index1]);
  d2 := StrToDate(List[Index2]);
  if d1 < d2 then
    Result := -1
  else if d1 > d2 then Result := 1
  else
    Result := 0;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  List: TStringlist;
begin
  ZufallsDatumHinzufuegen;
  List := TStringlist.Create;
  try
    ListBox1.Items.BeginUpdate;
    try
      List.Assign(ListBox1.Items);
      List.CustomSort(@CompareDates);
      ListBox1.Items.Assign(List);
    finally
      ListBox1.Items.EndUpdate;
    end;
  finally
    List.Free;
  end;
end;
Thomas
  Mit Zitat antworten Zitat