![]() |
Re: Stringgrid sortieren
Hier liegt doch gerade der Riesenvorteil des Funktionszeigers. Definiere Dir 2 Sortierfunktionen (auf- und absteigend) und rufe die jeweils passende auf.
|
Re: Stringgrid sortieren
Irgendwie versteh ich diese Funktion ueberhaupt nicht und das mit den Zeigern hab ich auch nicht verstanden..
Hab es jetzt eine naive und unflexible Notlösung.. :
Delphi-Quellcode:
//globale variable
mode := 'asc'; // sort descending function StringListSortCompare(List: TStringList; Index1, Index2: Integer): Integer; begin Result := AnsiCompareText(List[Index2], List[Index1]); // ka was hier passiert aber anscheinend der richtige Rückgabeparameter end; procedure TForm1.SortStringGrid(var GenStrGrid: TStringGrid; mode: String; ThatCol: Integer); const // Define the Separator TheSeparator = '@'; var CountItem, I, J, K, ThePosition: integer; MyList: TStringList; MyString, TempString: string; begin // Give the number of rows in the StringGrid CountItem := GenStrGrid.RowCount; //Create the List MyList := TStringList.Create; MyList.Sorted := False; try begin for I := 1 to (CountItem - 1) do MyList.Add(GenStrGrid.Rows[I].Strings[ThatCol] + TheSeparator + GenStrGrid.Rows[I].Text); //Sort the List if (mode='asc') then begin Mylist.Sort; mode := 'desc'; end else begin Mylist.CustomSort(StringListSortCompare); mode := 'asc'; end; for K := 1 to Mylist.Count do begin //Take the String of the line (K – 1) MyString := MyList.Strings[(K - 1)]; //Find the position of the Separator in the String ThePosition := Pos(TheSeparator, MyString); TempString := ''; {Eliminate the Text of the column on which we have sorted the StringGrid} TempString := Copy(MyString, (ThePosition + 1), Length(MyString)); MyList.Strings[(K - 1)] := ''; MyList.Strings[(K - 1)] := TempString; end; // Refill the StringGrid for J := 1 to (CountItem - 1) do GenStrGrid.Rows[J].Text := MyList.Strings[(J - 1)]; end; finally //Free the List MyList.Free; end; end; procedure TForm1.GridMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); Var i,breite,spalte:Integer; begin if y<=Grid.RowHeights[0] then begin breite:=0; For i:=0 To Grid.ColCount-1 Do Begin breite:=breite+Grid.ColWidths[i]; if breite>x Then Begin spalte:=i; Break; end; end; SortStringGrid(Grid, mode,spalte); end; end; Falls das mir einer nochmal erklaeren würde wie das automaitsch richtig sortiert wär ich froh ;) |
Re: Stringgrid sortieren
Schau doch mal in der Hilfe nach, was AnsiCompareText macht. Um anders herum zu sortieren, musst Du doch nur das Vorzeichen umdrehen.
Delphi-Quellcode:
Result := -AnsiCompareText(List[Index2], List[Index1]);
|
Re: Stringgrid sortieren
Hmm ok aber da ich ja immer nur
Delphi-Quellcode:
aufrufe muss die Fallunterscheidung ja innerhalb der Funktion "StringListSortCompare" geschehen.
Mylist.CustomSort(StringListSortCompare);
Wie stellt aber diese Funktion jetzt fest, ob sie jetzt zuletzt asc oder desc sortiert hat ? Der Debugger liefert mir ja auch keine Werte wo ich feststellen könnte was der Funktionsaufruf Mylist.CustomSort(StringListSortCompare); eigentlich für Parameter übergibt und was die Funktion zurückliefert.. :/ *auf der Leitung steh* |
Re: Stringgrid sortieren
Delphi-Quellcode:
Nun klarer?
if asc then
Mylist.CustomSort(StringListSortCompareAsc) else Mylist.CustomSort(StringListSortCompareDesc); |
Re: Stringgrid sortieren
Nur mal so am Rande bemerkt:
Es reicht völlig aus, den Sort immer nur aufsteigend erfolgen zu lassen! Je nach gewünschter Sortierichtung wird das Grid nach dem Sort mit auf-/oder absteigendem Listenindex gefüllt :zwinker: |
Re: Stringgrid sortieren
Das ist natürlich vollkommen richtig, aber mir geht es jetzt ums Prinzip :zwinker:
|
Re: Stringgrid sortieren
Das habe ich schon vermutet,
deshalb mein zaghaftes "Nur mal so am Rande bemerkt" :hi: |
Re: Stringgrid sortieren
Also habs jetzt so gelöst:
Delphi-Quellcode:
//global
NextTimeSort := 'asc'; SpalteZuletzt := 0; // sort ascending function StringListSortCompareAsc(List: TStringList; Index1, Index2: Integer): Integer; begin Result := -AnsiCompareText(List[Index2], List[Index1]); end; // sort descending function StringListSortCompareDesc(List: TStringList; Index1, Index2: Integer): Integer; begin Result := AnsiCompareText(List[Index2], List[Index1]); end; procedure TForm1.SortStringGrid(var GenStrGrid: TStringGrid; ThatCol: Integer); const // Define the Separator TheSeparator = '@'; var CountItem, I, J, K, ThePosition: integer; MyList: TStringList; MyString, TempString: string; begin // Give the number of rows in the StringGrid CountItem := GenStrGrid.RowCount; //Create the List MyList := TStringList.Create; MyList.Sorted := False; try begin for I := 1 to (CountItem - 1) do MyList.Add(GenStrGrid.Rows[I].Strings[ThatCol] + TheSeparator + GenStrGrid.Rows[I].Text); //Sort the List if (spalteZuletzt <> ThatCol) OR ((spalteZuletzt = ThatCol) AND (NextTimeSort = 'asc')) then begin Mylist.CustomSort(StringListSortCompareAsc); NextTimeSort := 'desc'; end else if ((spalteZuletzt = ThatCol) AND (NextTimeSort = 'desc')) then begin Mylist.CustomSort(StringListSortCompareDesc); NextTimeSort := 'asc'; end; for K := 1 to Mylist.Count do begin //Take the String of the line (K – 1) MyString := MyList.Strings[(K - 1)]; //Find the position of the Separator in the String ThePosition := Pos(TheSeparator, MyString); TempString := ''; {Eliminate the Text of the column on which we have sorted the StringGrid} TempString := Copy(MyString, (ThePosition + 1), Length(MyString)); MyList.Strings[(K - 1)] := ''; MyList.Strings[(K - 1)] := TempString; end; // Refill the StringGrid for J := 1 to (CountItem - 1) do GenStrGrid.Rows[J].Text := MyList.Strings[(J - 1)]; end; finally //Free the List MyList.Free; end; spalteZuletzt := ThatCol; end; procedure TForm1.GridMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); Var i,breite,spalte:Integer; begin if y<=Grid.RowHeights[0] then begin breite:=0; For i:=0 To Grid.ColCount-1 Do Begin breite:=breite+Grid.ColWidths[i]; if breite>x Then Begin spalte:=i; Break; end; end; SortStringGrid(Grid, spalte); end; end; |
Re: Stringgrid sortieren
Jetzt hast Du mich anscheinend verstanden :thumb:
|
Alle Zeitangaben in WEZ +1. Es ist jetzt 20:41 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 by Thomas Breitkreuz