Forum > General
How to sort objects in list
(1/1)
gabriel:
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 "<address of function(const Tobj;const Tobj):LongInt;Register>", expected "IComparer<classsort.Tobj>"
What is the correct syntax?
gabriel
Mr.Madguy:
Is it Generics module? You need to implement IComparer interface.
You should do it this way:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---uses Generics.Defaults; type TMyComparer = class(TComparer<TObj>) public function Compare(const Left, Right: TObj): Integer; override; end;
dbannon:
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 [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---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:
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 [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---unit sorter; {$mode objfpc}{$H+} interface uses Classes, SysUtils, person, generics.Defaults; type Tmycomparer = class(specialize Tcomparer<Tperson>) 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 pm ---Is it Generics module? You need to implement IComparer interface.
You should do it this way:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---uses Generics.Defaults; type TMyComparer = class(TComparer<TObj>) public function Compare(const Left, Right: TObj): Integer; override; end;
--- End quote ---
Mr.Madguy:
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 [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---function MyComparison(constref Left, Right: Tperson): Integer; ol.Sort(TComparer<Tperson>.Construct(MyComparison)); //Or this oneol.Sort(TDelegatedComparerFunc<Tperson>.Create(MyComparison));
Navigation
[0] Message Index