Autor Beitrag
del1312
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 190



BeitragVerfasst: Do 02.09.10 15:50 
Moderiert von user profile iconNarses: Abgetrennt von [url=www.delphi-forum.de/...?t=94741]hier[/url].

Ich hab da auch ne Frage zum sortieren von Listboxen!

Hab ne Listbox die ne Liste aus einer Textdatei liest, die sieht so aus:

1. bla bla
2. bla bla
3. bla bla
.
.
.
.
300.

usw

Wenn ich nun die Listbox auf Sortiert=true setze dann sortiert er mir die List aber das sieht dann leider so aus:

1.
10.
100.
101.
102.
.
.
.
11.
110.
112.

usw, wie kann ich die Listbox mit den fortlaufenden Zahlen sortieren? Danke für Tipps und Hinweise!
platzwart
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 1054
Erhaltene Danke: 78

Win 7, Ubuntu 9.10
Delphi 2007 Pro, C++, Qt
BeitragVerfasst: Do 02.09.10 15:57 
Du musst dazu einen eigenen Algorithmus schreiben und dann mit AlphaSort sortieren.

_________________
Wissenschaft schafft Wissenschaft, denn Wissenschaft ist Wissenschaft, die mit Wissen und Schaffen Wissen schafft. (myself)
gfehmke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 24



BeitragVerfasst: Do 02.09.10 20:07 
Hi

Vorm einfügen in die Listbox die gelesene Zeile mit Leerzeichen auf drei Zeichen verlängern
z.B.:

ausblenden Quelltext
1:
Zeile:=rightstr('   ' + Zeile,3);					


Bei Bedarf alle Zeilen der Listbox mit trim von Leerzeichen befreien.

G.
del1312 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 190



BeitragVerfasst: Do 02.09.10 20:16 
@platzwart: hm kannste mir da ein wenig helfen? weil gar nicht wo ich da anfangen soll.

@gfehmke: versteh nicht warum ich da 3 leerzeichen in jede zeile einfügen soll :oops:
platzwart
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 1054
Erhaltene Danke: 78

Win 7, Ubuntu 9.10
Delphi 2007 Pro, C++, Qt
BeitragVerfasst: Do 02.09.10 20:24 
Einfach mal nach 'AlphaSort' und 'OnCompare' suchen, da wird alles genau erklärt.

_________________
Wissenschaft schafft Wissenschaft, denn Wissenschaft ist Wissenschaft, die mit Wissen und Schaffen Wissen schafft. (myself)
del1312 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 190



BeitragVerfasst: Fr 03.09.10 13:12 
ich brauch da doch mehr hilfe, bin nicht so der delphi programmierer und fummel mich grad so rein. ich benötige immer beispiel code schnippsel, die nehm ich dann auseinander, schaue was passiert und lerne davon dann immer ;o) kann mir einer nen beispiel code posten, wie ich ne listbox sortieren kann nach zahlen ? also 1. 2. 3. usw ?
Luckie
Ehemaliges Mitglied
Erhaltene Danke: 1



BeitragVerfasst: Fr 03.09.10 14:12 
Hast du dir denn schon mal die genannten Suchbegriffe angeguckt?
bummi
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 1248
Erhaltene Danke: 187

XP - Server 2008R2
D2 - Delphi XE
BeitragVerfasst: Fr 03.09.10 15:22 
Mit beiliegendem Beispiel kannst Du Dir Sortieren nach beliebigen Kriterinen basteln ...
Nach Datum, 5. Buchstaben , was auch immer..
Als Beispiel für das was Du brauchst Compare4FirstIsInt


ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
unit Main;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls,dateUtils;

type

  CompareProc = Procedure (List: TStringList; Index1, Index2: Integer );
  TlistBox=Class(stdCtrls.TListBox)
      Procedure Sort(p:CompareProc);
  End;



  TForm5 = class(TForm)
    ListBox1: TListBox;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
    procedure ListBox1Click(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form5: TForm5;

implementation

{$R *.dfm}
function CompareZahl(const A, B: Double):Integer;
begin
  if A =B then Result := 0
  else if A < B then
    Result := -1
  else
    Result := 1;
end;

function Compare4Int(List: TStringList; Index1, Index2: Integer ):Integer;
var
  i1,i2:Integer;
begin
     if TryStrToInt(List[index1],i1) and TryStrToInt(List[index2],i2) then
       Result := CompareZahl(i1, i2)
     else Result := 0;
end;

function Compare4FirstIsInt(List: TStringList; Index1, Index2: Integer ):Integer;
var
  cnt1,cnt2:Integer;
  Procedure GetCount(Var cnt:Integer;Index:Integer);
    begin
       cnt := 0;
       while (Length(List[index]) > (cnt ))  and (List[Index][cnt + 1] in ['0'..'9']) do  inc(cnt);
    end;
begin
     GetCount(cnt1,Index1);
     GetCount(cnt2,Index2);
     if (cnt1>0) and (cnt2>0) then
       Result := CompareZahl(StrToInt(Copy(List[Index1],1,cnt1 )) , StrToInt(Copy(List[Index2],1,cnt2 )))
     else Result := 0;
end;

procedure TForm5.Button1Click(Sender: TObject);

begin
   ListBox1.Items.AddObject('4 was auch immer mit Objekt',TObject(4));
 //  ListBox1.Sort(@Compare4Int)
  ListBox1.Sort(@Compare4FirstIsInt);
end;

{ TlistBox }



procedure TlistBox.Sort(p:CompareProc);
var
  sl:TStringList;
begin
  sl := TStringList.Create;
  try
       sl.Assign(Items);
       sl.CustomSort(@p);
       Items.Assign(sl);
  finally
    sl.Free;
  end;

end;

procedure TForm5.ListBox1Click(Sender: TObject);
begin
   Showmessage(IntToStr(Integer(ListBox1.Items.Objects[ListBox1.ItemIndex])));
end;

end.
del1312 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 190



BeitragVerfasst: Fr 03.09.10 18:49 
Super vielen Dank bummi!!!!! Wo kann ich danke klicken!!!
gfehmke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 24



BeitragVerfasst: Fr 03.09.10 19:37 
Mit meinem Vogehen fügt man nicht drei Leerzeichen ein, sondern füllt vor den String soviele Leerzeichen ein, dass alle die Länge 3 haben.
Da das Leerzeichen beim Sortieren kleiner als '1' ist, greift das automatische Sortieren der Listbox wieder.
Durch Trim hat man dann wieder die originalen Zahlen.

Vielleicht ein bisschen 'quick and dirty' aber es funktioniert.

G.