Sorting Arrays






Sorting Arrays

Two useful static methods from Figure that deserve a closer look are Sort( ) and Reverse( ). These methods do what you think they would: Reverse( ) reverses the order of elements in the array, and Sort( ) sorts the elements in order. These are fully supported for arrays of the built-in C# types, such as string, so sorting an array of strings puts the elements in alphabetical order, and sorting an array of ints puts them in numeric order. Making the Sort( ) method work with your own classes is a bit trickier, as you must implement the IComparable interface (see Chapter 13 for more on interfaces). Figure demonstrates the use of these two methods to manipulate String objects.

Using Array.Sort and Array.Reverse

using System;

namespace ArraySortAndReverse
{
   public class Tester
   {
      public static void PrintMyArray( string[] theArray )
      {

         foreach ( string str in theArray )
         {
            Console.WriteLine( "Value: {0}", str );
         }
         Console.WriteLine( "\n" );
      }

      static void Main( )
      {
         String[] myArray =
            {
                "Proust", "Faulkner", "Mann", "Hugo"
            };

         PrintMyArray( myArray );
         Array.Reverse( myArray );
         PrintMyArray( myArray );

         String[] myOtherArray =
            {
                "We", "Hold", "These", "Truths",
                "To", "Be", "Self","Evident",
            };

         PrintMyArray( myOtherArray );
         Array.Sort( myOtherArray );
         PrintMyArray( myOtherArray );

      }
   }
}

The output looks like this:

    Value: Proust
    Value: Faulkner
    Value: Mann
    Value: Hugo

    Value: Hugo
    Value: Mann
    Value: Faulkner
    Value: Proust

    Value: We
    Value: Hold
    Value: These
    Value: Truths
    Value: To
    Value: Be
    Value: Self
    Value: Evident

    Value: Be
    Value: Evident
    Value: Hold
    Value: Self
    Value: These
    Value: To
    Value: Truths
    Value: We

The example begins by creating myArray, an array of strings with the words:

    "Proust", "Faulkner", "Mann", "Hugo"

This array is printed, and then passed to the Array.Reverse( ) method, where it is printed again to see that the array itself has been reversed:

    Value: Hugo
    Value: Mann
    Value: Faulkner
    Value: Proust

Similarly, the example creates a second array, myOtherArray, containing the words:

    "We", "Hold", "These", "Truths",
    "To", "Be", "Self", "Evident",

This is passed to the Array.Sort( ) method. Then Array.Sort( ) happily sorts them alphabetically:

    Value: Be
    Value: Evident
    Value: Hold
    Value: Self
    Value: These
    Value: To
    Value: Truths
    Value: We

Array.Sort( ) and Array.Reverse( ) are static methods, meaning you call them on the class, not the object, as discussed in Chapter 7. That means you don't call myArray.Reverse( ) to reverse the elements; instead, you call the static method and pass in the array as an argument, like this:

    Array.Reverse(myArray);




 Python   SQL   Java   php   Perl 
 game development   web development   internet   *nix   graphics   hardware 
 telecommunications   C++ 
 Flash   Active Directory   Windows