delphisortinggenericsdelphi-2010tobjectlist

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?


Solution

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