Source of TestStringArraySorting.java


  1: //TestStringArraySorting.java

  3: import java.util.Arrays;
  4: import java.util.Scanner;

  6: public class TestStringArraySorting
  7: {
  8:     public static void main(String[] args)
  9:     {
 10:         Scanner keyboard = new Scanner(System.in);

 12:         String[] stringArray = { "Harry", "Carla", "Al", "Rob", "Dave" };

 14:         System.out.println("\nPrinting names in the original order ...");
 15:         for (String s : stringArray)
 16:         {
 17:             System.out.print(s + " ");
 18:         }
 19:         System.out.print("\nPress Enter to continue ... ");
 20:         keyboard.nextLine();

 22:         System.out.println("\nSorted in the default way (alphabetical) ...");
 23:         Arrays.sort(stringArray);
 24:         for (String s : stringArray)
 25:         {
 26:             System.out.print(s + " ");
 27:         }
 28:         System.out.print("\nPress Enter to continue ... ");
 29:         keyboard.nextLine();

 31:         System.out.println("\nSorted by length ...");
 32:         Arrays.sort(stringArray, (s1, s2) -> s1.length() - s2.length());
 33:         for (String s : stringArray)
 34:         {
 35:             System.out.print(s + " ");
 36:         }
 37:         System.out.print("\nPress Enter to continue ... ");
 38:         keyboard.nextLine();

 40:         System.out.println
 41:         (
 42:             "\nSorted alphabetically using "
 43:             + "a different method ..."
 44:         );
 45:         Arrays.sort(stringArray, (s1, s2) -> s1.compareTo(s2));
 46:         for (String s : stringArray)
 47:         {
 48:             System.out.print(s + " ");
 49:         }
 50:         System.out.print("\nPress Enter to continue ... ");
 51:         keyboard.nextLine();

 53:         System.out.println("\nSorted in reverse alphabetical order ...");
 54:         Arrays.sort(stringArray, (s1, s2) -> s2.compareTo(s1));
 55:         for (String s : stringArray)
 56:         {
 57:             System.out.print(s + " ");
 58:         }
 59:         System.out.print("\nPress Enter to continue ... ");
 60:         keyboard.nextLine();

 62:         System.out.println("\nSorted by the last letter in the name ...");
 63:         Arrays.sort
 64:         (
 65:             stringArray,
 66:             (s1, s2) -> s1.substring(s1.length() - 1)
 67:             .compareTo(s2.substring(s2.length() - 1))
 68:         );
 69:         for (String s : stringArray)
 70:         {
 71:             System.out.print(s + " ");
 72:         }
 73:         System.out.print("\nPress Enter to continue ... ");
 74:         keyboard.nextLine();
 75:     }
 76: }
 77: /*  Output:

 79:     Printing names in the original order ...
 80:     Harry Carla Al Rob Dave
 81:     Press Enter to continue ...

 83:     Sorted in the default way (alphabetical) ...
 84:     Al Carla Dave Harry Rob
 85:     Press Enter to continue ...

 87:     Sorted by length ...
 88:     Al Rob Dave Carla Harry
 89:     Press Enter to continue ...

 91:     Sorted alphabetically using a different method ...
 92:     Al Carla Dave Harry Rob
 93:     Press Enter to continue ...

 95:     Sorted in reverse alphabetical order ...
 96:     Rob Harry Dave Carla Al
 97:     Press Enter to continue ...

 99:     Sorted by the last letter in the name ...
100:     Carla Rob Dave Al Harry
101:     Press Enter to continue ...
102: */