DEV Community

Tiago Soczek
Tiago Soczek

Posted on

How to Sort Two Arrays with Array.Sort in .NET

The method Array.Sort(array, array) has been available since .NET Framework 2.0, but I recently discovered it. It allows sorting a pair of arrays, with the first array used as keys and the second as values:

var keys = ['A','C','B'];
var values = [1,3,2];

Array.Sort(keys, values);

// keys = ['A','B','C']
// values = [1,2,3]
Enter fullscreen mode Exit fullscreen mode

The result is that both arrays are sorted, preserving the relationship between keys and values. Additionally, you can provide a custom Comparer to define your own sorting logic.

While Array.Sort is not commonly seen in "commercial" code nowadays, I found this feature quite interesting and versatile!

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay