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

/**
 * Prompts the user for a sentence and outputs the words of the sentence in ASCII order
 * and case-insensitive ASCII order.
 *
 * @author Drue Coles
 */
public class ComparatorDemo2 {

    public static void main(String[] args) {

        // Read a line of text and split into an array of words.
        Scanner in = new Scanner(System.in);
        System.out.print("Enter a sentence: ");
        String[] words = in.nextLine().split(" ");
        System.out.println();
        
        // Remove punctuation from words.
        for (int i = 0; i < words.length; i++) {
            words[i] = words[i].replaceAll("[,.?!]", "");
        }
        
        // Sort words in ASCII order and output.
        System.out.print("Words in ASCII order: ");
        Arrays.sort(words);
        for (String t : words) {
            System.out.print(t + " ");
        }
        System.out.println();

        // A comparator to compare words without regard for case.
        class MyStringComparator implements Comparator<String> {

            @Override
            public int compare(String s1, String s2) {
                return s1.compareToIgnoreCase(s2);
            }
        }

        // Sort words in case-insensitive ASCII order and output.
        System.out.print("    Case-insensitive: ");
        Arrays.sort(words, new MyStringComparator());
        for (String t : words) {
            System.out.print(t + " ");
        }
        System.out.println();
    }
}