Recent

Author Topic: SortColRow Function  (Read 9438 times)

fredycc

  • Sr. Member
  • ****
  • Posts: 264
SortColRow Function
« on: April 01, 2010, 08:59:12 pm »
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.
« Last Edit: April 01, 2010, 09:02:58 pm by fredycc »

faber

  • Guest
Re: SortColRow Function
« Reply #1 on: April 01, 2010, 10:41:46 pm »
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

  • Sr. Member
  • ****
  • Posts: 483
Re: SortColRow Function
« Reply #2 on: April 01, 2010, 11:01:20 pm »
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

  • Sr. Member
  • ****
  • Posts: 264
Re: SortColRow Function
« Reply #3 on: April 02, 2010, 01:40:25 am »
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. :)
« Last Edit: April 02, 2010, 01:46:43 am by fredycc »

jesusr

  • Sr. Member
  • ****
  • Posts: 483
Re: SortColRow Function
« Reply #4 on: April 02, 2010, 06:07:34 am »
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.

eny

  • Hero Member
  • *****
  • Posts: 1628
Re: SortColRow Function
« Reply #5 on: April 02, 2010, 08:52:42 am »
Or you could use the Math unit with the CompareValue functions:

Code: [Select]
//...
  try
    result := CompareValue(StrToInt(Cells[ACol,ARow], StrToInt(Cells[BCol,BRow]);
  Except
    result := AnsiCompareText(Cells[ACol,ARow], Cells[BCol,BRow]);
  end;
//...
All posts based on: Win10 (Win64); Lazarus 2.0.10 'stable' (x64) unless specified otherwise...

fredycc

  • Sr. Member
  • ****
  • Posts: 264
Re: SortColRow Function
« Reply #6 on: April 03, 2010, 05:06:36 am »
I think eny that your solution is better, thanks  ;D

try
   Result := CompareValue(StrToInt(Cells[ACol,ARow]), StrToInt(Cells[BCol,BRow]));
Except
   Result:=AnsiCompareText(Cells[ACol,ARow], Cells[BCol,BRow]);
end;

 

TinyPortal © 2005-2018