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] 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!
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!