Einzelnen Beitrag anzeigen

Benutzerbild von himitsu
himitsu

Registriert seit: 11. Okt 2003
Ort: Elbflorenz
43.168 Beiträge
 
Delphi 12 Athens
 
#6

Re: Sortierung sortiert nicht richtig?!

  Alt 2. Dez 2003, 09:16
Ein Hallöle von http://www.FrankNStein.de/Smiley-Wolke.gif,

nach Zahlen sortieren => vorm/beim sortieren die Strings z.B. in Integer umwandeln.

Delphi-Quellcode:
Procedure SortStringGrid(SG: TStringGrid; SortCol: Byte; inclFixedCol: Boolean = True);
  Var FC, I, I2, I3, Z, Z2, C, C2: Integer;
    S: String;

  Begin
    If (SortCol >= SG.ColCount) or (inclFixedCol and (SortCol < SG.FixedCols)) Then Exit;
    If inclFixedCol Then FC := 0 Else FC := SG.FixedCols;
    For I := SG.FixedRows to SG.RowCount - 2 do
      For I2 := I + 1 to SG.RowCount - 1 do Begin
        Val(SG.Cells[SortCol, I], Z, C);
        Val(SG.Cells[SortCol, I2], Z2, C2);
        If ((C or C2 = 0) and (Z > Z2)) or
          ((C or C2 <> 0) and (SG.Cells[SortCol, I] > SG.Cells[SortCol, I2])) Then
          For I3 := FC to SG.ColCount - 1 do Begin
            S := SG.Cells[I3, I];
            SG.Cells[I3, I] := SG.Cells[I3, I2];
            SG.Cells[I3, I2] := S;
          End;
      End;
  End;
Delphi-Quellcode:
{inclusive der fixierten Spalten sortieren}
SortStringGrid(StringGrid1, Spalte);
SortStringGrid(StringGrid1, Spalte, True);

{fixierte Spalten nicht mit sortieren}
SortStringGrid(StringGrid1, Spalte, False);
wenn die Sortierreinfolge geändert werden soll - einfache die Vergleichsoperatoren austauschen (">", "<"):
Code:
[b]If[/b] ((C [b]or[/b] C2 = 0) [b]and[/b] (Z [color=red][b]>[/b][/color] Z2)) [b]or[/b]
  ((C [b]or[/b] C2 <> 0) [b]and[/b] (SG.Cells[SortCol, I] [color=red][b]>[/b][/color] SG.Cells...
[ADD]
Ach ja, diese Prozedur sortiert nicht die Überschrift (fixierte Zeilen) des StringGrids und wenn erwünscht auch nicht die fixierten Spalten.


http://www.FrankNStein.de/Smiley-Kuss.gif * * http://www.FrankNStein.de/Smiley-Spinne.gif * * * http://www.FrankNStein.de/Smiley-Winken.gif
Garbage Collector ... Delphianer erzeugen keinen Müll, also brauchen sie auch keinen Müllsucher.
my Delphi wish list : BugReports/FeatureRequests
  Mit Zitat antworten Zitat