Recent

Author Topic: Sorting a TList  (Read 9563 times)

Neuromancer

  • New Member
  • *
  • Posts: 44
    • My personal website
Sorting a TList
« on: May 22, 2010, 08:35:01 am »
Hi,

I am trying to sort elements in a TList using the Sort function :

Code: [Select]
function CompareInactiveWorkers(ASquare1, ASquare2 : Pointer) : integer;
begin
Result := TSquare(ASquare2).FUnemployment - TSquare(ASquare1).FUnemployment;
end;
Squares := TList.Create;
Squares.Add(TSquare.Create);
Squares.Sort(@CompareInactiveWorkers);

I get a compilation error :
Code: [Select]
Error: Incompatible type for arg no. 1: Got "<address of function(TAbstractSquare,TAbstractSquare):LongInt;Register>", expected "<procedure variable type of function(Pointer,Pointer):LongInt;Register>"

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 11286
  • Debugger - SynEdit - and more
    • wiki
Re: Sorting a TList
« Reply #1 on: May 22, 2010, 09:13:02 am »
Do you maybe have an overloaded version of that function (same name, but diff param)?

Otherwise:
- fpc version ?
- $mode (delphi, objfpc,...)?

bflm

  • Jr. Member
  • **
  • Posts: 54
    • Free Pascal Random Bits
Re: Sorting a TList
« Reply #2 on: May 22, 2010, 09:15:11 am »
The error doesn't make sense to me in the supposed by the snippet context. It may help to narrow the problem down to a minimal, but complete example and repost it here.

Neuromancer

  • New Member
  • *
  • Posts: 44
    • My personal website
Re: Sorting a TList
« Reply #3 on: May 22, 2010, 10:35:42 am »
Do you maybe have an overloaded version of that function (same name, but diff param)?
No

Otherwise:
- fpc version ?
- $mode (delphi, objfpc,...)?
FPC version 2.4.0
{$mode objfpc}

Full source code here : http://www.brilland.org/uploads/ecoprog2.zip
« Last Edit: February 20, 2012, 12:31:40 am by Neuromancer »

theo

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1932
Re: Sorting a TList
« Reply #4 on: May 22, 2010, 10:43:12 am »
Do you maybe have an overloaded version of that function (same name, but diff param)?
No

Why do you say no? economy.pas around line 157:

Code: [Select]
function CompareInactiveWorkers(ASquare1, ASquare2 : TAbstractSquare) : integer;
begin
    Result := 0;
end;

function CompareInactiveWorkers(ASquare1, ASquare2 : Pointer) : integer;
begin
    Result := TSquare(ASquare2).FUnemployment - TSquare(ASquare1).FUnemployment;
end;



Neuromancer

  • New Member
  • *
  • Posts: 44
    • My personal website
Re: Sorting a TList
« Reply #5 on: May 22, 2010, 11:19:16 am »
It works nows. Thanks.  ::)
« Last Edit: May 22, 2010, 02:10:45 pm by Neuromancer »

 

TinyPortal © 2005-2018