Sort Examples

The general syntax for this method is:

Array.Sort(arrayName)

This method puts the elements of the array in ascending or alphabetical order.

Example

Sort the entire array.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
string[] pets = new string[]
{
   "Alyce",
   "Thisbe",
   "Francis",
   "Scratch",
   "Beatrice",
   "Shadow",
   "Beatrice"
};

Console.WriteLine(pets.GetValue(1));

Array.Sort(pets);

Console.WriteLine(pets.GetValue(1));

Output

Thisbe
Beatrice

Sorting Sections

Sorting a section of an array is possible, too. The general syntax is as follows:

Array.Sort(arrayName, startingIndex, rangeIndex);

Try It!

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
string[] pets = new string[]
{
   "Alyce",
   "Thisbe",
   "Francis",
   "Scratch",
   "Beatrice",
   "Shadow",
   "Beatrice"
};


Array.Sort(pets, 0, 3);