![]() |
Sortieralgorythmus funktioniert nicht ganz
Habe folgenden Sortieralgorhytmus im Swissdelphicenter gefunden:
Delphi-Quellcode:
Dieser funktioniert auch gut, aber leider sortiert er Zahlen absteigend (im StringGrid sind also die niedrigsten Zahlen ganz unten).
type
TMoveSG = class(TCustomGrid); // reveals protected MoveRow procedure {...} procedure SortGridByCols(Grid: TStringGrid; ColOrder: array of Integer); var i, j: Integer; Sorted: Boolean; function Sort(Row1, Row2: Integer): Integer; var C: Integer; begin C := 0; Result := AnsiCompareStr(Grid.Cols[ColOrder[C]][Row1], Grid.Cols[ColOrder[C]][Row2]); if Result = 0 then begin Inc(C); while (C <= High(ColOrder)) and (Result = 0) do begin Result := AnsiCompareStr(Grid.Cols[ColOrder[C]][Row1], Grid.Cols[ColOrder[C]][Row2]); Inc(C); end; end; end; begin if SizeOf(ColOrder) div SizeOf(i) <> Grid.ColCount then Exit; for i := 0 to High(ColOrder) do if (ColOrder[i] < 0) or (ColOrder[i] >= Grid.ColCount) then Exit; j := 0; Sorted := False; repeat Inc(j); with Grid do for i := 0 to RowCount - 2 do if Sort(i, i + 1) > 0 then begin TMoveSG(Grid).MoveRow(i + 1, i); Sorted := False; end; until Sorted or (j = 1000); Grid.Repaint; end; procedure TForm1.Button1Click(Sender: TObject); begin { Sort rows based on the contents of two or more columns. Sorts first by column 1. If there are duplicate values in column 1, the next sort column is column 2 and so on...} SortGridByCols(StringGrid1, [1, 2, 0, 3, 4]); end; Wo muss ich diesen Algo ändern, damit er aufsteigend sortiert (aber die Buchstaben weiterhin so wie bisher (die kann er sortieren))? Bin für jede Hilfe dankbar :bounce1: |
Re: Sortieralgorythmus funktioniert nicht ganz
Zitat:
Wenn Du statt AnsiCompareStr nun eine eigene Funktion benutzt z.B. MyCompareStr, kannst du jede gewünschte Sortierung programmieren. z.B.
Delphi-Quellcode:
Falls Deine Frage beantwortet ist, nicht vergessen mit dem http://www.delphipraxis.net/template...t_answered.gif - Button oben auf der Seite die Frage als beantwortet zu markieren.
function MyCompareStr(s1, s2: string):Integer;
begin // Bedingung Rückgabewert // S1 > S2 > 0 // S1 < S2 < 0 // S1 = S2 = 0 // Result := AnsiCompareStr(s1, s2); if s1 > s2 then Result := 1 else if s1 < s2 then Result := -1 else Result := 0; end; function Sort(Row1, Row2: Integer): Integer; var C: Integer; begin C := 0; Result := MyCompareStr(Grid.Cols[ColOrder[C]][Row1], Grid.Cols[ColOrder[C]][Row2]); if Result = 0 then begin Inc(C); while (C <= High(ColOrder)) and (Result = 0) do begin Result := MyCompareStr(Grid.Cols[ColOrder[C]][Row1], Grid.Cols[ColOrder[C]][Row2]); Inc(C); end; end; end; Danke |
Re: Sortieralgorythmus funktioniert nicht ganz
Zahlen behandelt die Sortierfunktion in der gezeigten Form wie Zeichenketten - dass heißt 100 kommt vor 90, aber 20 kommt nach 10. Eine absteigende Sortierung kann ich mir da schwerlich vorstellen.
Grüße vom marabu |
Re: Sortieralgorythmus funktioniert nicht ganz
Zitat:
Delphi-Quellcode:
function MyCompareStr(s1, s2: string):Integer;
begin // Bedingung Rückgabewert // S1 > S2 > 0 // S1 < S2 < 0 // S1 = S2 = 0 try if StrToInt(s1) > StrToInt(s2) then Result := 1 else if StrToInt(s1) < StrToInt(s2) then Result := -1 else Result := 0; except Result := AnsiCompareStr(s1, s2); end: end; |
Alle Zeitangaben in WEZ +1. Es ist jetzt 16:13 Uhr. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz