+ 1

How can i sort my array?

18th Oct 2016, 6:43 PM
ringman
2 Answers
+ 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 😇
18th Oct 2016, 8:01 PM
Erwin Mesias
Erwin Mesias - avatar
+ 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
18th Oct 2016, 8:58 PM
Ivan G
Ivan G - avatar