/**************************************************************************
 * Written by Ben Mathew
 * for Cornell University CS 100 Summer 2001
 *
 * This file examines Java's String comparison method, String.compareTo()
 */

public class StringSorter
{
    public static void main (String [] args)
    {
	
        String first, middle, last; //Strings for comparison
        String swapper; //temp swap String
        
        //some strings to sort
        first = "Or last?";
        middle = "or last?";
        last = "Am I first?";
        
        //print out the unsorted strings
        System.out.println(first + " " + middle + " " + last);
        

        //The way that String.compareTo() works is that it takes two
        //Strings, in the following format:
        //String1.compareTo(String2)
        //now, if String1 appears later in the alphabet than String2,
        //you will get a positive value from this. If String1 is
        //before String2 alphabetically, then you will get a negative
        //value. If they are equal, you will get zero. Notice that if
        //two strings differ only in capitalization, then upper-case
        //will come before lower-case, as this example shows.


        //loop while strings are not sorted
        while (first.compareTo(middle) >= 0 || middle.compareTo(last) >= 0)
            {
                //if first is after middle, then switch
                if (first.compareTo(middle) > 0)
                    {
                        swapper = middle;
                        middle = first;
                        first = swapper;
                    }
                //if middle is after last, then switch
                if (middle.compareTo(last) > 0)
                    {
                        swapper = last;
                        last = middle;
                        middle = swapper;
                    }
            }
        
        //print sorted strings
        System.out.println(first + " " + middle + " " + last);

        //Remember that the numbers from compareTo will often be strange
        //and not necessarily nice like -1 or 0 or 1, so use the inequality
        //operators, except for == 0 to make sure you perform the comparisons
        //correctly. Later on, when you learn more Java, you will be able to
        //sort whole lists of words that are given to you using just a few
        //lines of code similar to those above.
        //
        //- b

    }
    
}
