Performing a simple sort of text strings that are all the same case, e.g. upper-case, is no more difficult in .NET than it is for sorting integers. The .NET method Array.Sort comes to our rescue by being able to sort objects that are derived from the base System.Array type.
This program sorts an array of unsorted strings in to the normal alphabetical order.
using System;
class StringSort
{
static void Main()
{
// allocate a small array of ten text strings
string[] stringValues = { "JUSTIN", "CATHERINE", "AMANDA", "NICHOLAS", "GARETH", "CHRISTOPHER", "PAUL", "ROBERT", "GAIL", "KEVYN" };
// sort the array in to natural alphabetical order
Array.Sort(stringValues);
// output the sorted array
Console.WriteLine(String.Join(", ", stringValues));
}
}
AMANDA, CATHERINE, CHRISTOPHER, GAIL, GARETH, JUSTIN, KEVYN, NICHOLAS, PAUL, ROBERT
The console output clearly shows that the input array of people’s names, stringValues, has been sorted according to the English alphabet.
The Array.Sort method is capable of sorting any object in a variety of ways that has been derived from the System.Array base type.