Recent

Author Topic: How to sort generic list  (Read 8549 times)

Blaazen

  • Hero Member
  • *****
  • Posts: 3237
  • POKE 54296,15
    • Eye-Candy Controls
How to sort generic list
« on: August 06, 2012, 07:46:26 pm »
Hi,

I have class and generic list:
Code: [Select]
TMyClass =class
  Name: string;
  Count: Integer;
end;

TMyList = specialize TFPGObjectList<TMyClass>;

I would like to Sort by Count, Name or whatever and TFPGObjectList contains public method Sort:
Code: [Select]
procedure Sort(Compare: TCompareFunc);
How and where should I declare its parameter (Compare: TCompareFunc) ?
(Note that TCompareFunc is function returning Integer.)

I'm getting error messages like this:
Quote
umain.pas(425,63) Error: Incompatible type for arg no. 1: Got "<procedure variable type of function(const TMyClass;const TMyClass):LongInt of object;Register>", expected "TFPGObjectList$1$crc7683A43D.<procedure variable type of function(const TMyClass;const TMyClass):LongInt;Register>"
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/

Leledumbo

  • Hero Member
  • *****
  • Posts: 8747
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: How to sort generic list
« Reply #1 on: August 06, 2012, 07:58:06 pm »
Quote
How and where should I declare its parameter (Compare: TCompareFunc) ?
(Note that TCompareFunc is function returning Integer.)
Anywhere as long as it's part of an object (e.g. a method). I usually create a descendant class containing this Compare method and overload the Sort method (or create a new one with clearer names like SortByCount, SortByName, etc) with no parameter which calls that original Sort method with the specified Compare method.

Blaazen

  • Hero Member
  • *****
  • Posts: 3237
  • POKE 54296,15
    • Eye-Candy Controls
Re: How to sort generic list
« Reply #2 on: August 06, 2012, 09:22:41 pm »
OK, I have class:

Code: [Select]
TMyList = class(specialize TFPGObjectList<TMyClass>)
  procedure Sort(Compare: TCompareFunc);
  function SortNameAsc(const A1,A2:TMyClass): Integer;
end;
and
Code: [Select]
procedure TMyList.Sort(Compare: TCompareFunc);
begin
  inherited Sort(@SortBWNameAsc);
end;

I'm still getting the same error.
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/

eny

  • Hero Member
  • *****
  • Posts: 1634
Re: How to sort generic list
« Reply #3 on: August 06, 2012, 09:59:52 pm »
The compare function doesn't look like a method function but a plain old pascal function.
So I guess you'll have to do something like:
Code: [Select]
function CompareNameAsc(const Item1, Item2: TMyClass): Integer;
begin
  // Do comparison for -1, 0, 1
end;

{ TMyList }

procedure TMyList.SortByName;
begin
  Sort(@CompareNameAsc);
end;
The benefit is that you can now add a typed compare function instead of the untyped one where you had to do a typecast for comparison.
All posts based on: Win10 (Win64); Lazarus 2.0.10 'stable' (x64) unless specified otherwise...

Blaazen

  • Hero Member
  • *****
  • Posts: 3237
  • POKE 54296,15
    • Eye-Candy Controls
Re: How to sort generic list
« Reply #4 on: August 06, 2012, 10:22:41 pm »
@ The compare function doesn't look like a method function but a plain old pascal function.

Thanks, that was it.
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/

Leledumbo

  • Hero Member
  • *****
  • Posts: 8747
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: How to sort generic list
« Reply #5 on: August 06, 2012, 11:44:54 pm »
Oops, sorry. I misinterpret the error message.

 

TinyPortal © 2005-2018