Recent

Author Topic: Sorting stringgrid  (Read 7796 times)

Hansvb

  • Hero Member
  • *****
  • Posts: 736
Sorting stringgrid
« on: May 21, 2015, 10:10:14 pm »
Until now i handled the sorting of a stringgrid myself. I just saw that there is a columnClicksorts. When this is true columns with only stringa sort fine. Columns with numbers sort wrong.

When i look into the wiki i find CompareCells with an example:
Adjusted a little:


Code: [Select]
  // Result will be either <0, =0, or >0 for normal order.
  result := StrToIntDef(StringGrid_ctrl_tabel.Cells[ACol,ARow],0)-StrToIntDef(StringGrid_ctrl_tabel.Cells[BCol,BRow],0);
  // For inverse order, just negate the result (eg. based on grid's SortOrder).
  if StringGrid_ctrl_tabel.SortOrder = soDescending then
    result := -result;


Now only the columns with numbers sort fine. The columns with strings don't sort anymore.

How to sort strings en numbers ?

sfeinst

  • Full Member
  • ***
  • Posts: 235
Re: Sorting stringgrid
« Reply #1 on: May 21, 2015, 10:30:48 pm »
I'm not sure what event you are overriding to perform the sort, but I'm going to guess that you should probably check the column (guessing ACol and BCol) to see if it is a number column or a string column.  If number, then using what you have would work, but if a string column then you want to use the normal sorting not your function.

Blaazen

  • Hero Member
  • *****
  • Posts: 3241
  • POKE 54296,15
    • Eye-Candy Controls
Re: Sorting stringgrid
« Reply #2 on: May 21, 2015, 10:46:48 pm »
TStringGrid can sort columns and rows with string. You don't need to assign OnCompareCells for it.
You need to assign OnCompareCells for non-string sorting only.

Sort with string is:
Code: [Select]
StringGrid1.SortColRow(IsColumn: Boolean; Index, From, To: Integer); and StringGrid1.OnCompareCells is not assigned.
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

typo

  • Hero Member
  • *****
  • Posts: 3051
Re: Sorting stringgrid
« Reply #3 on: May 21, 2015, 11:51:09 pm »
Or you could use function UTF8NaturalCompareText from NaturalSortUnit:

http://sourceforge.net/projects/lazarusfiles/files/naturalsort.zip/download

Even internal numbers in the strings will be correctly sorted.
« Last Edit: May 22, 2015, 12:00:53 am by typo »

PeterX

  • Sr. Member
  • ****
  • Posts: 428
Re: Sorting stringgrid
« Reply #4 on: January 19, 2025, 01:05:47 pm »
Or you could use function UTF8NaturalCompareText from NaturalSortUnit:

http://sourceforge.net/projects/lazarusfiles/files/naturalsort.zip/download

Even internal numbers in the strings will be correctly sorted.
...  "this topic has not been posted in for at least 120 days."
Yes, I understood ..


The NaturalCompareText works for me, thanks for that !

But now there's no chance to sort with
Code: Pascal  [Select][+][-]
  1. TSortOrder = (soAscending, soDescending);

???
usually using latest Lazarus release version with Windows 10 or 11

PeterX

  • Sr. Member
  • ****
  • Posts: 428
Re: Sorting stringgrid
« Reply #5 on: January 19, 2025, 01:38:34 pm »
Okay, learning by doing, and carefully reading helps a lot ..

I missed these lines from the example code:
Code: Pascal  [Select][+][-]
  1.   if StringGrid_ctrl_tabel.SortOrder = soDescending then
  2.     result := -result;

Now soDescending works.  8)
usually using latest Lazarus release version with Windows 10 or 11

 

TinyPortal © 2005-2018