sort an array in csharp

To sort an array in C#, you can use the built-in Array.Sort() method. The Array.Sort() method uses an implementation of the QuickSort algorithm to sort the elements of an array in ascending order. Here's an example of how to use it:

main.cs
int[] array = { 3, 2, 1, 5, 4 };

Array.Sort(array);

foreach (int i in array)
{
    Console.WriteLine(i);
}
109 chars
9 lines

This will output:

main.cs
1
2
3
4
5
10 chars
6 lines

You can also sort arrays in descending order by using a custom IComparer implementation and passing it as a second argument to the Array.Sort() method.

gistlibby LogSnag