How to sort objects in list
lazarus / 21-General
----------------------------------------------------------------------
gabriel · 2021-05-05 10:26
Hey Forum
I am new to Lazarus/freepascal. I try to sort a Tlist of objects. But
I get this error:
classsort.lpr(45,11) Error: Wrong number of parameters specified for
call to "comparefunc".
If I change the line to: ol.Sort(@comparefunc); then I get this
error:
classsort.lpr(45,23) Error: Incompatible type for arg no. 1: Got
"
",
expected "IComparer"
What is the correct syntax?
gabriel
----------------------------------------------------------------------
Mr.Madguy · 2021-05-05 11:34
Is it Generics module? You need to implement IComparer interface.
You should do it this way:
Code: Pascal [Select][+][-]window.onload = function(){var x1 =
document.getElementById("main_content_section"); if (x1) { var x =
document.getElementsByClassName("geshi");for (var i = 0; i uses
Generics.Defaults; type TMyComparer = class(TComparer) public
function Compare(const Left, Right: TObj): Integer; override; end;
----------------------------------------------------------------------
dbannon · 2021-05-05 11:40
Hmm, just guessing here, your compare function seems to expect two
TObjects, I use plain pointers and the docs indicate that what sort()
expects. eg my code looks like this -
Code: Pascal [Select][+][-]window.onload = function(){var x1 =
document.getElementById("main_content_section"); if (x1) { var x =
document.getElementsByClassName("geshi");for (var i = 0; i function
LastChangeSorter( Item1: Pointer; Item2: Pointer) : Integer;begin
result := CompareStr(PNote(Item1)^.LastChange,
PNote(Item2)^.LastChange);end;
Note the pointers get '^' to say "the thing they point to", if you
have been looking at Delphi code, you don't need them. You can tell
FPC to treat the code as Delphi by setting the mode to Delphi but
better to treat pointers as pointers IMHO.
Davo
----------------------------------------------------------------------
gabriel · 2021-05-06 15:15
Thank you Mr Madguy
I have tried to implement this ( see below) bu I get this error
sorter.pas(13,14) Error: function header doesn't match the previous
declaration "compare(const Tperson;const Tperson):LongInt;"
But I have not declared enything before.
Do you have a clue ?
Code: Pascal [Select][+][-]window.onload = function(){var x1 =
document.getElementById("main_content_section"); if (x1) { var x =
document.getElementsByClassName("geshi");for (var i = 0; i unit
sorter; {$mode objfpc}{$H+} interface uses Classes, SysUtils, person,
generics.Defaults; type Tmycomparer = class(specialize
Tcomparer) public function compare(const left, right:
Tperson) : integer; override;end; implementation function
Tmycomparer.compare( const left, right: Tperson) : integer;begin if
left.navn > right.navn then result := 1 else if left.navn <
right.navn then result := -1 else result := 0;end; end.
Quote from: Mr.Madguy on May 05, 2021, 01:34:07 pmIs it Generics
module? You need to implement IComparer interface.
You should do it this way:
Code: Pascal [Select][+][-]window.onload = function(){var x1 =
document.getElementById("main_content_section"); if (x1) { var x =
document.getElementsByClassName("geshi");for (var i = 0; i uses
Generics.Defaults; type TMyComparer = class(TComparer) public
function Compare(const Left, Right: TObj): Integer; override; end;
----------------------------------------------------------------------
Mr.Madguy · 2021-05-07 08:12
I don't have latest Lazarus version installed on my computer now. I
used Delphi example. But according to this you should try to change
"const" to "constref".
And you can actually try to work with comparison function via this
syntax:
Code: Pascal [Select][+][-]window.onload = function(){var x1 =
document.getElementById("main_content_section"); if (x1) { var x =
document.getElementsByClassName("geshi");for (var i = 0; i function
MyComparison(constref Left, Right: Tperson):
Integer; ol.Sort(TComparer.Construct(MyComparison)); //Or
this oneol.Sort(TDelegatedComparerFunc.Create(MyComparison));