This content originally appeared on DEV Community and was authored by Tiago Soczek
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]
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!
This content originally appeared on DEV Community and was authored by Tiago Soczek
Tiago Soczek | Sciencx (2025-01-18T15:39:26+00:00) How to Sort Two Arrays with Array.Sort in .NET. Retrieved from https://www.scien.cx/2025/01/18/how-to-sort-two-arrays-with-array-sort-in-net/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.