Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delphi trouble: Sorting a Tobjectlist<>

I want to sort my generic tobjectlist using the built-in sort method.

here is what I do:

//create the list object
myList := TObjectList<MyType>.Create(false);   

[...] //populate the list with unsorted entries

//sort the list
myList.sort(@Comparer);

[...]//store sorted results back to array

myList.Destroy;

my Comparer function looks like this:

function Comparer(Item1, Item2 : pointer):integer;
begin
  result := myCompare(item1, item2);
end;

According to the specs, it should work like this.

I get an compiler error E2250 No overloaded version of 'Sort' exist with these parameters (exact wording differs, i use a non english version of RAD Studio)

I have no idea why this should not be valid Pascal - does anyone of you have insight to share on this?

like image 953
sum1stolemyname Avatar asked Jan 24 '11 16:01

sum1stolemyname


1 Answers

You are almost there. Since I don't know what MyType is you may need to change the call to your myCompare function.

myList.Sort(TComparer<MyType>.Construct(
   function (const L, R: MyType): integer
   begin
     result := myCompare(L, R);
   end
));
like image 73
Leonardo Herrera Avatar answered Oct 09 '22 07:10

Leonardo Herrera