Einzelnen Beitrag anzeigen

Delphi-Coder

Registriert seit: 8. Jan 2003
Ort: Greifswald / Stralsund
151 Beiträge
 
Delphi 7 Enterprise
 
#1

Sortierung sortiert nicht richtig?!

  Alt 2. Dez 2003, 08:25
Hallo DP'ler,

ich hab mir vom swissdelphicenter einen Algorithmus zum Sortieren eines StringGrids nach Spalten geholt! Hier ist der Link!
Das funktioniert auch ganz gut. Er soll ein StringGrid nach einer Spalte sortieren, in der verschiedene Zahlen stehen (1-stellig bis 4-stellig). Komischerweise setzt er die 2-stelligen Zahlen an das Ende (also hinter die 4-stellugen) des StringGirds!

Woran könnte das liegen? bzw. Wie kann ich das ändern?

Vielen Dank im voraus!

MfG
Delphi-Coder

P.S.: ich hab doch nochmal den Code eingefügt!

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

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