Go Premium for a chance to win a PS4. Enter to Win

x
Solved

Sorting a TStringGrid

Posted on 2000-04-21
Medium Priority
238 Views
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
Question by:Softtech

LVL 2

Expert Comment

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

ITugay earned 200 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

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

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

Featured Post

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…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Suggested Courses
Course of the Month11 days, 21 hours left to enroll

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

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