Reverse
Examples¶
The general syntax for this method is:
Array.Reverse(arrayName)
This method reverses the order of the indices.
Example
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.Reverse(pets);
Console.WriteLine(pets.GetValue(1));
|
Output
Thisbe
Shadow
Reversing Sections¶
Reversing a section of an array is possible, too. The general syntax is as follows:
Array.Reverse(arrayName, startingIndex, rangeIndex);
Example
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.Reverse(pets, 0, 3);
|
If you are looking for reverse alphabetical order,
you will need to use this along with the Sort
method.