Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Object-Pascal / Delphi-Language (https://www.delphipraxis.net/32-object-pascal-delphi-language/)
-   -   Delphi bubble sort, stringlist und stringgrid sortiern (https://www.delphipraxis.net/33422-bubble-sort-stringlist-und-stringgrid-sortiern.html)

yankee 6. Nov 2004 19:39


bubble sort, stringlist und stringgrid sortiern
 
Hi @ll.
Mein Ziel ist es, ein StringGrid zu sortieren. In ein paar Spaltzen des Stringgrids kommen nur integers rein und deswegen habe ich gedacht, dass ich diese spalten mit bubblesort sortiere und habe mir bubble sort etwas umgeschrieben. Und auch die SortStringprocedure, die ich von delphiswiss habe:

Delphi-Quellcode:
procedure TForm_search.BubbleSort(items: TStringList);
var
  done: boolean;
  i, n,i0,i1: integer;
  Dummy: string;
begin
  n := Items.Count;
  repeat
    done := true;
    for i := 0 to n - 2 do
    begin
{Problem:}i0 :=strtoint(copy(items.Strings[i], 0, pos('@',items.Strings[i])-1));
      i1 :=strtoint(copy(items.Strings[i+1], 0, pos('@',items.Strings[i+1])-1));
      //Hier wurde was geändert
      if (i1 > i0) then
      begin
        Dummy := items.Strings[i];
        Items.Strings[i] := items.Strings[i+1];
        Items.Strings[i + 1] := Dummy;
        done := false
      end;
    end;
  until done
end;

procedure TForm_search.SortStringGrid(var GenStrGrid: TStringGrid; ThatCol: Integer; const number: Boolean);
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 not number then Mylist.Sort
      else BubbleSort(MyList);

      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;
Nur leider funzt das nicht so, wie ich mir das vorstelle (die stringspalten sortieren ist kein Problem, aber bei den integer Spalten gibt es hin und wieder mal ein Problem in der markierten Zeile... Und zwar: ''' is not a valid integer value
Ich habe mal versucht zur convertierung val zu nehmen und habe mir, wenn der integerwert nicht gültig war, den string ausgegeben. Im String war eine 0 (0 kommt allerdings verdammt häufig vor, es scheint aber nur an sehr wenigen Stellen der Fehler aufzutreten), also habe ich dann i0/i1 im entsprechenden Fall einfach manuell auf 0 gesetzt. dann gab' es keine exeption mehr, aber auch keine Spur von sortierung...

EDIT: Die Zahlen im StringGrid werden aus einem record gelesen und im record ist es ein intger. Also würde ich theoretisch auch deswegen ausschließen, dass der intger wirklich kein integer ist...

Mario 9. Nov 2004 08:47

Re: bubble sort, stringlist und stringgrid sortiern
 
Schuss ins Blaue: In Deinem String kommt das Zeichen @ nicht nur als Seperator vor und somit ermittelt Pos einfach die falsche Position?

yankee 9. Nov 2004 19:24

Re: bubble sort, stringlist und stringgrid sortiern
 
Danke, habe die Lösung schon. Habe das Progi grundsätzlich umgestaltet:
Mein Code-Libary Thread


Alle Zeitangaben in WEZ +1. Es ist jetzt 17:13 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz