Source of Sort2.java


  1: // Fig. 19.9: Sort2.java
  2: // Using a Comparator object with algorithm sort.
  3: import java.util.List;
  4: import java.util.Arrays;
  5: import java.util.Collections;
  6: 
  7: public class Sort2 
  8: {
  9:    private static final String suits[] = 
 10:       { "Hearts", "Diamonds", "Clubs", "Spades" };
 11: 
 12:    // output List elements
 13:    public void printElements()
 14:    {
 15:       List< String > list = Arrays.asList( suits ); // create List
 16: 
 17:       // output List elements
 18:       System.out.printf( "Unsorted array elements:\n%s\n", list );
 19: 
 20:       // sort in descending order using a comparator
 21:       Collections.sort( list, Collections.reverseOrder() ); 
 22: 
 23:       // output List elements
 24:       System.out.printf( "Sorted list elements:\n%s\n", list );
 25:    } // end method printElements
 26:  
 27:    public static void main( String args[] )
 28:    {
 29:       Sort2 sort2 = new Sort2();
 30:       sort2.printElements();
 31:    } // end main
 32: } // end class Sort2
 33: 
 34: 
 35: /**************************************************************************
 36:  * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and               *
 37:  * Pearson Education, Inc. All Rights Reserved.                           *
 38:  *                                                                        *
 39:  * DISCLAIMER: The authors and publisher of this book have used their     *
 40:  * best efforts in preparing the book. These efforts include the          *
 41:  * development, research, and testing of the theories and programs        *
 42:  * to determine their effectiveness. The authors and publisher make       *
 43:  * no warranty of any kind, expressed or implied, with regard to these    *
 44:  * programs or to the documentation contained in these books. The authors *
 45:  * and publisher shall not be liable in any event for incidental or       *
 46:  * consequential damages in connection with, or arising out of, the       *
 47:  * furnishing, performance, or use of these programs.                     *
 48:  *************************************************************************/