The underlying question at [WayBack] … Does anyone know of any array sort method similar to this for Delphi (with the two array parameters, key and value with a comparer method) … – Ugochukwu Mmaduekwe – Google+ was basically this:
Why is there no
class procedure TArray<T>.Sort(Keys: array of T; var Values: array of T; const Comparer: IComparer<T>); overload;
From my answer:
No it is not there as the method that does the actual sort does not accommodate for it:
class procedure TArray.QuickSort<T>(var Values: array of T; const Comparer: IComparer<T>; L, R: Integer);
You could write a helper for it so that similarly to C#, they all end in
public static void Sort(Array keys, Array items, int index, int length, IComparer comparer)
In case you need to look at the external TrySZSort: https://github.com/dotnet/coreclr/blob/master/src/classlibnative/bcltype/arrayhelpers.cpp#L268
In other cases, C# uses an IntrospectionSort implemented at
Or you could try to use this overload of TArray.Sort Method (array of T):
class procedure Sort<T>(var Values: array of T; const Comparer: IComparer<T>); overload;
It was introduced in Delphi 2009.
Your Comparer then needs to extract the key of each element and implement IComparer<T>.
However, this will never sort the
Keys
array.
–jeroen