Delphi trouble: Sorting a Tobjectlist<>

sum1stolemyname picture sum1stolemyname · Jan 24, 2011 · Viewed 8.8k times · Source

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?

Answer

Leonardo Herrera picture Leonardo Herrera · Jan 24, 2011

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
));