The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 2,087 other followers

Delphi: Why is there no class procedure TArray.Sort(Keys: array of T; var Values: array of T; const Comparer: IComparer);

Posted by jpluimers on 2019/12/26

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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

 
%d bloggers like this: