//TestStringArraySorting.java

import java.util.Arrays;
import java.util.Scanner;

public class TestStringArraySorting
{
    public static void main(String[] args)
    {
        Scanner keyboard = new Scanner(System.in);

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

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

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

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

        System.out.println
        (
            "\nSorted alphabetically using "
            + "a different method ..."
        );
        Arrays.sort(stringArray, (s1, s2) -> s1.compareTo(s2));
        for (String s : stringArray)
        {
            System.out.print(s + " ");
        }
        System.out.print("\nPress Enter to continue ... ");
        keyboard.nextLine();

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

        System.out.println("\nSorted by the last letter in the name ...");
        Arrays.sort
        (
            stringArray,
            (s1, s2) -> s1.substring(s1.length() - 1)
            .compareTo(s2.substring(s2.length() - 1))
        );
        for (String s : stringArray)
        {
            System.out.print(s + " ");
        }
        System.out.print("\nPress Enter to continue ... ");
        keyboard.nextLine();
    }
}
/*  Output:

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

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

    Sorted by length ...
    Al Rob Dave Carla Harry
    Press Enter to continue ...

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

    Sorted in reverse alphabetical order ...
    Rob Harry Dave Carla Al
    Press Enter to continue ...

    Sorted by the last letter in the name ...
    Carla Rob Dave Al Harry
    Press Enter to continue ...
*/
