Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Sonstige Fragen zu Delphi (https://www.delphipraxis.net/19-sonstige-fragen-zu-delphi/)
-   -   Delphi Zahlen in ListBox1 Sortieren (https://www.delphipraxis.net/13247-zahlen-listbox1-sortieren.html)

Pseudemys Nelsoni 14. Dez 2003 20:01


Zahlen in ListBox1 Sortieren
 
hoi, nehmen wir mal an ich habe eine listbox in den alle zahlen von 1-1000 sind.
wie sortier ich das, dass es so aussieht:

1
2
3
..
10
..
100
..
1000


?

ich ich einfach Sorted benutze, wird es so sortiert:

1
10
100
2
3

toms 14. Dez 2003 20:16

Re: Zahlen in ListBox1 Sortieren
 
Hi,

Du könntest die Items der TListbox einer TStringList zuweisen und diese dann mittels
CustomSort Methode sortieren lassen.
Am Schluss werden die TStringList Items wieder der TListBox zugewiesen:


Delphi-Quellcode:
function CompareInt(List: TStringList; Index1, Index2: Integer): Integer;
// Wichtig: Keine Fehlerbehandlung, wenn ein Item der List kein Integerwert ist!!
var
  d1, d2: Integer;
begin
  d1 := StrToInt(List[Index1]);
  d2 := StrToInt(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
  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;


Alle Zeitangaben in WEZ +1. Es ist jetzt 09:37 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz