Recent

Author Topic: String Grid SORT functionality  (Read 1083 times)

Aqdam1978@yahoo.com

  • New Member
  • *
  • Posts: 13
String Grid SORT functionality
« on: August 31, 2021, 04:41:47 am »
Hello,

I am using a StringGrid component in my app, I just noticed when I am clicking on header column it sorts based on simple comparison (maybe just a ">" operand).

My question is:
Is there any "logical sort" functionality in Lazarus at all? Is it possible to replace current sort algorithm in the StringGrid component with a logical sort?

Thanks

winni

  • Hero Member
  • *****
  • Posts: 2702
Re: String Grid SORT functionality
« Reply #1 on: August 31, 2021, 09:04:05 am »
Hallo!

You can connect with the event onCompareCells of the stringGrid and then  for an integer sort:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.StringGrid1CompareCells(Sender: TObject; ACol, ARow, BCol,
  2.  BRow: Integer; var Result: integer);
  3.  
  4. begin
  5. with StringGrid1 do
  6.    begin
  7.     result := StrToInt64Def(Cells[ACol,ARow],0)-StrToInt64Def(Cells[BCol,BRow],0);
  8.      if SortOrder = soDescending then result := -result;
  9. end;// with
  10. end;
  11.  
  12.  

For unsigned values you use
 
Code: Pascal  [Select][+][-]
  1. result := StrToQWordDef(Cells[ACol,ARow],0)-StrToQwordDef(Cells[BCol,BRow],0);
  2.  

And for float values:
Code: Pascal  [Select][+][-]
  1. result := round(StrToFloatDef(Cells[ACol,ARow],0)-StrToFloatDef(Cells[BCol,BRow],0));
  2.  
       

Winni

 

TinyPortal © 2005-2018