Sort and search an ArrayList : ArrayList « Collections Data Structure « C# / C Sharp






Sort and search an ArrayList

Sort and search an ArrayList
  
/*
C#: The Complete Reference 
by Herbert Schildt 

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/

// Sort and search an ArrayList. 
 
using System; 
using System.Collections; 
 
public class SortSearchDemo { 
  public 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"); 
 
    // 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)); 
  } 
}


           
         
    
  








Related examples in the same category

1.Add items to ArrayList and use foreach loop to check
2.Add array to ArrayList
3.Remove range from ArrayList
4.Clone an ArrayList
5.CopyTo, ToArray(), ToArray(typeof(String))
6.Using foreach to loop through ArrayList and use Indexer of ArrayList
7.Check InvalidCastException when using foreach loop with ArrayList
8.Binary Search an ArrayList
9.Inserting into an ArrayList by index
10.Checks time needed for list operations using an ArrayList implementationChecks time needed for list operations using an ArrayList implementation
11.ArrayList Demo: hold classArrayList Demo: hold class
12.illustrates the use of ArrayList properties and methodsillustrates the use of ArrayList properties and methods
13.illustrates the use of an ArrayList that contains objects of the Car classillustrates the use of an ArrayList that contains objects of the Car class
14.illustrates the use of ArrayLists 2illustrates the use of ArrayLists 2
15.Convert an ArrayList into an arrayConvert an ArrayList into an array
16.Demonstrate ArrayListDemonstrate ArrayList
17.Search for the first occurrence of the duplicated value
18.Search for the first occurrence of the duplicated value in the last section of the ArrayList
19.Search for the first occurrence of the duplicated value in a section of the ArrayList
20.Search for the first occurrence of the duplicated value in a small section at the end of the ArrayList
21.Convert a ArrayList object to a array
22.Represents a loose abstraction of an arraylist, wheres the buffer array is available for public access.