Forum > General
SortColRow Function
fredycc:
Hi, I have test SortColRow function for a StringGrid for sort many rows and works great but when I use the same functions for sort integers the result is:
1
10
100
1000
1001
101
1010
1011
1012
2
There is a way to specified that I want to sort like a numbers and not strings :-\.
Thanks.
faber:
StringGrid can only sort text (AnsiCompareText).
You can sort with your own sorting procedure, find QuickSort from grids.pas and use it, change AnsiCompareText to > or <.
But in that case you'll have to protect your app.
jesusr:
in fact, stringgrid can do custom sorting by using event OnCompareCells event, while handling this event one can use the ACol,ARow,BCol,BRow arguments to convert strings to numbers and use them to do the compare.
if "A" cell is less than "B" cell, result should be negative, if "A" cell is greater than "B" cell, result should be positive, and if both cells are equal, result should be 0
fredycc:
thanks for your suggestions, I have modified the procedure and works :), sucessfully.
I'll modify a little the code of this procedure:
function TCustomStringGrid.DoCompareCells(Acol, ARow, Bcol, BRow: Integer
): Integer;
begin
if Assigned(OnCompareCells) then
Result:=inherited DoCompareCells(Acol, ARow, Bcol, BRow)
else begin
try
if StrToInt(Cells[ACol,ARow]) < StrToInt(Cells[BCol,BRow]) then
result := -1
else if StrToInt(Cells[ACol,ARow]) > StrToInt(Cells[BCol,BRow]) then
result := 1
else result := 0;
Except
Result:=AnsiCompareText(Cells[ACol,ARow], Cells[BCol,BRow]);
end;
if SortOrder=soDescending then
result:=-result;
end;
end;
I don't know if is the better way but works for me, any suggestios will be welcome; and thanks again. :)
jesusr:
The only thing that stringgrid can safely assume is that what it does already, that cells contain strings and any other situation should be handled by developers either by extending the stringgrrid or by using the OnCompareCells event, which exists precisely for this situation.
Navigation
[0] Message Index
[#] Next page