package com.mindprod.example;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Random;
import static java.lang.System.*;
/**
* Demonstrate the use of Sun's sort built into Java.
* <p/>
* invoke with:
* C:
* cd \\ java.exe com.mindprod.example.TestSunSort
*
* @author Roedy Green, Canadian Mind Products
* @version 1.0 2010-12-28
* @since 2010-12-28
*/
public class TestSunSort
{
public static void main( String[] args )
{
int N = 100000;
String[] anArray = new String[ N ];
Random wheel = new Random( 149 );
for ( int i = 0; i < anArray.length; i++ )
{
anArray[ i ] = "A" + ( ( wheel.nextInt() & Integer.MAX_VALUE ) % 10000 );
}
out.println( "Start SunSort items=" + N );
long start = System.currentTimeMillis();
Arrays.sort( anArray, new DescendingStringComparator() );
long stop = System.currentTimeMillis();
out.println( "Elapsed:" + ( stop - start ) + " millis" );
for ( int i = 0; i <= 20; i++ )
{
out.println( anArray[ i ] );
}
}
/**
* Compare two Strings, case insensitively in reverse order.
* <p/>
* Defines an alternate sort order for String.
*/
private static class DescendingStringComparator implements Comparator<String>
{
/**
* Compare two Strings, case insensitively in reverse order.
* Defines an alternate sort order for String with JDK 1.5+ generics.
* Compare two String Objects.
* Compares descending whole object case insensitively.
* Informally, returns (a-b), or +ve if a comes after b.
*
* @param a first String to compare
* @param b second String to compare
*
* @return +ve if a>b, 0 if a==b, -ve if a<b
*/
public final int compare( String a, String b )
{
return b.compareToIgnoreCase( a );
}
}
}