Sort method in ArrayList

In this article I will explain the sort method of arraylist.
  • 1982

Sort method can be used to perform sorting in arraylist's element in an ascending order. ArrayList takes the elements as an object type so we can add whether inter or string or any other data type and then perform the sort methods on this.

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);

            }

        }

    }

}


Output

 

sort1.jpg
 

 

Further Readings
 

Ask Your Question

Got a programming related question? You may want to post your question here
 

 

Categories

More Articles

© 2019 DotNetHeaven. All rights reserved.