Autor Beitrag
Melmier
Ehemaliges Mitglied
Erhaltene Danke: 1



BeitragVerfasst: Sa 22.09.12 13:07 
Guten Tag,

da ich nicht mehr weiterkomme hoffe ich hier Hilfe zu finden. Mein Problem ist, eine Stringgrid nach Größe der Integer der festgelegten zu sortieren (soweit kein Problem), aber den Skript den ich benutze sortiert nur nach dem ersten Wert der Variable, also von z.B. "15" liest dieser nur die "1" ein, und somit ist "15" weniger als "3", da "3" größer ist als "1".
Ich möchte, dass in der Tabelle die "15" vor der "3", "5", oder "8" steht.
Ich weiß dass es immer ärgerlich ist einen fertigskript zu verwenden, und andere zu bitten diesen zu überschauen, vor allem weil dies enorm zeitaufwändig ist. Falls irgendjemand trotzdem bereit wäre, wäre ich enorm dankbar! Im folgendem der Skript (siehe www.entwickler-ecke....ortieren_110253.html, da ist der selbe Skript, ich benutzte dort die Änderung der Sortierungsrichtung).

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:
procedure 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 - 1do
        MyList.Add(GenStrGrid.Rows[I].Strings[ThatCol] + TheSeparator +
          GenStrGrid.Rows[I].Text);
      //Sort the List
      Mylist.Sort;

      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 := (CountItem-1Downto 1 Do
        GenStrGrid.Rows[GenStrGrid.Rowcount-J].Text := MyList.Strings[(J - 1)];
    end;
  finally
    //Free the List
    MyList.Free;
  end;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  // Sort the StringGrid1 on the second Column
  // StringGrid1 nach der 1. Spalte sortieren
  SortStringGrid(StringGrid1, 1);
end;




Schon im voraus, vielen Dank für jeden der sich die Mühe macht das allein durchzulesen, Entschuldigung für etwaige Fehler.

Moderiert von user profile iconNarses: Quote- durch Delphi-Tags ersetzt und URL-Tags eingefügt
Melmier
Ehemaliges Mitglied
Erhaltene Danke: 1



BeitragVerfasst: So 28.10.12 14:48 
/push

Keiner eine Idee? Schade, aber trotzdem danke fürs befassen.
mandras
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 431
Erhaltene Danke: 107

Win 10
Delphi 6 Prof, Delphi 10.4 Prof
BeitragVerfasst: So 28.10.12 16:55 
Customsort ist z.B. Dein Freund

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
 function OwnCompare (List: TStringList; Index1, Index2: Integer): Integer;
 var i1,i2:integer;
 begin
  i1:=StrToIntDef (List[Index1],0);
  i2:=StrToIntDef (List[Index2],0);
  if i1<i2 then result := -1;
  if i1=i2 then result := 0;
  if i1>i2 then result := 1;
 end;

procedure TForm1.FormCreate(Sender: TObject);
var l:tstringlist;
begin
 l:=tstringlist.create;
 l.add ('4');
 l.add ('15');
 l.Add('22');
 l.CustomSort (OwnCompare);
end;
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19312
Erhaltene Danke: 1747

W11 x64 (Chrome, Edge)
Delphi 11 Pro, Oxygene, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: Mo 29.10.12 13:03 
Wenn es nicht unbedingt ein StringGrid sein muss, würde sich auch eine TVirtualStringTree anbieten. Die kann sich selbst anhand einer Vergleichsroutine sortieren.