Solved

Sorting a TStringGrid

Posted on 2000-04-21
3
235 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

691 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