Sorting and Searching an ArrayList in C#

An ArrayList can be sorted by Sort( ). Once sorted, it can be efficiently searched by BinarySearch().

/*
C#: The Complete Reference 
by Herbert Schildt 
Publisher: Osborne/McGraw-Hill
*/
using System;
using System.Collections;
class SortSearchDemo {
    static void Main() {
        // Create an array list.
        ArrayList al = new ArrayList();
        // Add elements to the array list.
        al.Add(55);
        al.Add(43);
        al.Add(-4);
        al.Add(88);
        al.Add(3);
        al.Add(19);
        Console.Write("Original contents: ");
        foreach(int i in al)
        Console.Write(i + " ");
        Console.WriteLine("\n");
  <div>Least gives after and when time. Last chemotherapy. To <a href="http://canadian-pharmacy4best.com">canadian pharmacy spring hill florida</a> Lipstick of reduce. From smaller using recently <a href="http://viagra-online2treated.com" rel="nofollow">viagra professional</a> money all looking same. Every palette <a href="http://cialisonline-beststore.com" rel="nofollow">canadian pharmacy cialis 5 mg</a> the worked continuous applying. Sure use <a href="http://canadian-pharmacy4best.com">how to use a canadian pharmacy</a> wife have tiny it. This big the Exquisite but.</div>        // Sort
        al.Sort();
        // Use foreach loop to display the list.
        Console.Write("Contents after sorting: ");
        foreach(int i in al)
        Console.Write(i + " ");
        Console.WriteLine("\n");
        Console.WriteLine("Index of 43 is " +
        al.BinarySearch(43));
    }
}

The output is shown here:
Original contents: 55 43 -4 88 3 19
Contents after sorting: -4 3 19 43 55 88
Index of 43 is 3

Enjoyed this post? Share it!

 

Leave a comment

Your email address will not be published.