/**
 * Test the Bunny's compareTo method more stringently, by passing in objects
 * that aren't of type Bunny (or, rather, NamedSortableBunny). Rest of class is
 * same as previous version.
 * 
 * Lecture 23, Thu Mar 30 2006, CPSC 111
 */
public class Sorter
{   
    /**
     * Sort an array of any objects that implement the Comparable interface.
     */
    public static void sort( Comparable[] objects ) // static version
    {
        
        Comparable temp; 
        int min;
        // select location of next sorted value
        for( int i = 0; i < objects.length - 1; i++ )
        {
            min = i;
            // find the smallest value in the remainder of
            // the array to be sorted
            for( int j = i + 1; j < objects.length; j++ )
            {                
                 if( objects[ j ].compareTo( objects[ min ] ) < 0 )
                {
                    min = j;
                }
            }
            // swap two values in the array
            temp = objects[ i ];
            objects[ i ] = objects[ min ];
            objects[ min ] = temp;
        }
        
        System.out.println( "Printing sorted result" );
        for( int i = 0; i < objects.length; i++ )
        {
            System.out.println( objects[ i ] );
        }
    }
    
    public static void main( String[] args )
    {
        // int[] numbers = {16,3,19,8,12};
        Integer[] numbers = new Integer[ 5 ];
        numbers[ 0 ] = new Integer( 16 );
        numbers[ 1 ] = new Integer( 3 );
        numbers[ 2 ] = new Integer( 19 );
        numbers[ 3 ] = new Integer( 8 );
        numbers[ 4 ] = new Integer( 12 );
        
        Sorter.sort( numbers );
        
        String[] strings = new String[4];
        strings[0] = new String("the");
        strings[1] = new String("ame");
        strings[2] = new String("cat");
        strings[3] = new String("dog");
        
        Sorter.sort(strings);       
        
        NamedSortableBunny[] bunnies = new NamedSortableBunny[4];
        NamedSortableBunny peter = new NamedSortableBunny(3,6,1,"Peter");
        NamedSortableBunny emily = new NamedSortableBunny(3,4,5,"Emily");
        NamedSortableBunny darlene = new NamedSortableBunny(3,6,1,"Darlene");
        NamedSortableBunny aaron = new NamedSortableBunny(3,4,5,"Aaron");
        
        bunnies[0] = peter;
        bunnies[1] = emily;
        bunnies[2] = darlene;
        bunnies[3] = aaron;
        
        Sorter.sort(bunnies); 
        
        // check whether a direct compareTo call works, both for Bunny and
        // nonBunny (i.e. String) objects
        
        darlene.compareTo(aaron);
        darlene.compareTo("ILoveBunnies");
    }
    
}
