Solved

Sorting a TStringGrid

Posted on 2000-04-21
3
234 Views
Last Modified: 2010-04-04
What is the best/easiest way to sort the contents of a TStringGrid?  I have a TStringGrid which has 100 rows and 5 columns.  I want the rows to be sorted based upon the contents of column #1.

Thanks.
0
Comment
Question by:Softtech
3 Comments
 
LVL 2

Expert Comment

by:hhamster
ID: 2738984
Use any sort algorithm on the 1st column. Some of them can do it on the same TStringGrid component and some can do it by copying sorted values to a new instance of the TStringGrid component. Which one you choose result in the amount coding and the speed of executing.

Do something like this:
i,j.minimuRow : LongInt;
tempRow : TStrings;

for i:=0 to RowCount-2 do
begin
  // sg - your StringGrid
  // 1 for column 1
  currentValue:=sg.Cells(i,1);

  // supose the min row
  minimumRow:=i;

  // search till rest for real min row
  for j:=i+1 to RowCount - 1 do
    if sg.Cells(j,1)<sg.Cells(minimumRow,1) then
      minimumRow:=j;

  // swap if some other row is min
  if minimumRow<>i then
  begin
    tempRow:=sg.Rows[i];
    sg.Rows[i]:=sg.Rows[minimumRow];
    sg.Rows[minimumRow]:=tempRow;
  end;
end;

0
 
LVL 9

Accepted Solution

by:
ITugay earned 50 total points
ID: 2739548
Hi all.
It must work fast, because it used native TStringList sorting algorythm.

SG - your TStringGrid.
-------------
procedure TForm1.SpeedButton1Click(Sender: TObject);
var L : TStringList;
    G : TStringGrid;
    I : integer;
begin
   G := TStringGrid.Create(nil);
   G.RowCount:=SG.RowCount;
   G.ColCount:=SG.ColCount;

   L := TStringList.Create;
   L.Sorted:=true;
   for I:=0 to SG.RowCount-1 do
   begin
      L.AddObject(SG.Cells[2,I],pointer(I)); // 2- column to sort
      G.Rows[I].Assign(SG.Rows[I]);
   end;
   for I:=0 to L.Count-1 do
     SG.Rows[I].Assign(G.Rows[integer(L.Objects[I])]);

   L.Free;
   G.Free;
end;

-----
Igor
0
 
LVL 1

Expert Comment

by:DValery
ID: 2742735
Hi Softtech,
I did answer on like question:

http://www1.experts-exchange.com/bin/Q.10326262
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question