+ 1
How can i sort my array?
2 Respuestas
+ 3
You can sort your array in ASCENDING ORDER
using the code below:
Array.Sort(arrayName);
But if you want to Sort it in DESCENDING ORDER,
you just have to REVERSE the order of your array AFTER sorting it in ascending order, like in the code below:
Array.Sort(arrayName);
Array.Reverse(arrayName);
Hope that helps 😇
+ 3
You can also use LINQ methods:
arrayName.OrderBy(x => x).ToArray();
There is also OrderByDescending method.
With these methods you can sort collections (arrays/lists or any other IEnumerable realizations) even by nested objects properties:
var arr = new string[] { "aa", "bbb", "c"}; // we have an array of strings of different length
var sortedArr = arr.OrderBy(x => x.Length).ToArray(); // and now we have our arr sorted by string length