Reverse method in ArrayList
In this article I will explain the Reverse method of arraylist.
Sort method can be used to perform sorting in arraylist's element in an ascending order. Now In order to print or see the elements in descending order we can reverse the sorted element in the arraylist using the reverse method.
Example
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
ArrayList numbers = new ArrayList();
numbers.Add(10);
numbers.Add(22);
numbers.Add(3);
numbers.Add(5);
numbers.Add(9);
Console.WriteLine("The elements of the arraylist are:");
foreach (int i in numbers)
{
Console.WriteLine(i);
}
numbers.Sort();
Console.WriteLine("After sorting the elements of the arraylist are:");
foreach (int i in numbers)
{
Console.WriteLine(i);
}
numbers.Reverse();
Console.WriteLine("After apply the reverse method the number of element in the arraylist are:");
foreach (int i in numbers)
{
Console.WriteLine(i);
}
}
}
}
|
Output
Further Readings
You may also want to read these related articles.
Ask Your Question
Got a programming related question? You may want to post your question here