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,…


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


Print Share Comment Cite Upload Translate Updates
APA

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/

MLA
" » How to Sort Two Arrays with Array.Sort in .NET." Tiago Soczek | Sciencx - Saturday January 18, 2025, https://www.scien.cx/2025/01/18/how-to-sort-two-arrays-with-array-sort-in-net/
HARVARD
Tiago Soczek | Sciencx Saturday January 18, 2025 » How to Sort Two Arrays with Array.Sort in .NET., viewed ,<https://www.scien.cx/2025/01/18/how-to-sort-two-arrays-with-array-sort-in-net/>
VANCOUVER
Tiago Soczek | Sciencx - » How to Sort Two Arrays with Array.Sort in .NET. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/01/18/how-to-sort-two-arrays-with-array-sort-in-net/
CHICAGO
" » How to Sort Two Arrays with Array.Sort in .NET." Tiago Soczek | Sciencx - Accessed . https://www.scien.cx/2025/01/18/how-to-sort-two-arrays-with-array-sort-in-net/
IEEE
" » How to Sort Two Arrays with Array.Sort in .NET." Tiago Soczek | Sciencx [Online]. Available: https://www.scien.cx/2025/01/18/how-to-sort-two-arrays-with-array-sort-in-net/. [Accessed: ]
rf:citation
» How to Sort Two Arrays with Array.Sort in .NET | Tiago Soczek | Sciencx | 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.

You must be logged in to translate posts. Please log in or register.