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)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Community—every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple “thank you” goes a long way—express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay