Could any one tell me how i can sort a StringGrid?
I would like to sort on the first col and not the header.
ex:
==================
Mime | Type
==================
8mf | aplication/data
ini | aplication/data
gif | aplication/data
Thanks
Christian
try this unit.
{+------------------------------------------------------------
| Unit GridSort
|
| Version: 1.0 Last modified: 20.06.97, 14:53:47
| Author : P. Below
| Project: Common utilities
| Description:
| Provides a method to sort a stringgrid by column
+------------------------------------------------------------}
Unit GridSort;
Interface
Uses Classes, Grids;
Procedure SortStringgrid( Grid: TStringGrid; byColumn: LongInt;
ascending: Boolean );
Implementation
Uses Messages, SysUtils, Forms, Controls;
Procedure SortStringgrid( Grid: TStringGrid; byColumn: LongInt;
ascending: Boolean );
Procedure ExchangeGridRows( i, j: Integer );
Var
k: Integer;
Begin
With Grid Do
For k:= 0 To ColCount-1 Do
Cols[k].Exchange(i,j);
End;
procedure QuickSort(L, R: Integer);
var
I, J: Integer;
P: String;
begin
repeat
I := L;
J := R;
P := Grid.Cells[byColumn, (L + R) shr 1];
repeat
while CompareStr(Grid.Cells[byColumn, I], P) < 0 do Inc(I);
while CompareStr(Grid.Cells[byColumn, J], P) > 0 do Dec(J);
if I <= J then
begin
If I <> J Then
ExchangeGridRows( I, J );
Inc(I);
Dec(J);
end;
until I > J;
if L < J then QuickSort(L, J);
L := I;
until I >= R;
end;
Procedure InvertGrid;
Var
i, j: Integer;
Begin
i:= Grid.Fixedrows;
j:= Grid.Rowcount-1;
While i < j Do Begin
ExchangeGridRows( I, J );
Inc( i );
Dec( j );
End; { While }
End;
Begin
Screen.Cursor := crHourglass;
Grid.Perform( WM_SETREDRAW, 0, 0 );
try
QuickSort( Grid.FixedRows, Grid.Rowcount-1 );
If not ascending Then
InvertGrid;
finally
Grid.Perform( WM_SETREDRAW, 1, 0 );
Grid.Refresh;
Screen.Cursor := crDefault;
end;
End;
Initialization
End { GridSort }.
Peter Below (TeamB) 10011...@compuserve.com)
No e-mail responses, please, unless explicitly requested!
Thanks but how do i use it???
Christian
Peter Below (TeamB) <10011...@compuXXserve.com> a écrit dans le message :
VA.00003ea6.011f23c8@noname...
Thanks but how do i use it???
>>
Christian, please don't let your newsreader paste in the whole message
you're replying to (and which everyone can read the original of). You
managed to use 3K just to say the above!
PhR
Oh Jesus, a total newbie <G>. Save the code to a unit named
Gridsort.pas in your project directory. Add GridSort to your form units
Uses clause. To sort stringgrid1 by column 1 ascending you would add
the following code to whatever event handler that you intend to execute
the sort from (e.g. a buttons OnClick handler):
SortStringgrid( stringgrid1, 1, true );
Note that the routine does a simple alphabetic sort and it uses the
ANSI collating sequence, not the one for the current locale. To get it
to sort locale-sensitive replace CompareStr with AnsiCompareStr. The
sort is case sensitive. If you want it to be case insensitive use
CompareText (or AnsiCompareText) instead of CompareStr.
he he :c) yap im a newbie.... you got to start some where!
Christian