C# Sort Array of Strings
To sort an array of strings lexicographically in ascending order in C#, call Array.Sort() method and pass the array as argument.
The order of the elements in the original array will be modified.
Example
In the following example, we take an array of strings arr
, and sort elements of this array in ascending order using Array.Sort().
Program.cs
</>
Copy
using System;
namespace CSharpExamples {
class Program {
static void Main(string[] args) {
string[] arr = { "mango", "cherry", "apple", "lemon", "banana" };
Array.Sort( arr );
for (int i = 0; i < arr.Length; i++) {
Console.WriteLine( "arr[{0}]: {1}", i, arr[i] );
}
}
}
}
Output
arr[0]: apple
arr[1]: banana
arr[2]: cherry
arr[3]: lemon
arr[4]: mango
Conclusion
In this C# Tutorial, we learned how to sort an array of strings, with examples.