/* * [TestSunSort.java] * * Summary: Demonstrate the use of Sun's sort built into Java. * * Copyright: (c) 2010-2017 Roedy Green, Canadian Mind Products, http://mindprod.com * * Licence: This software may be copied and used freely for any purpose but military. * http://mindprod.com/contact/nonmil.html * * Requires: JDK 1.8+ * * Created with: JetBrains IntelliJ IDEA IDE http://www.jetbrains.com/idea/ * * Version History: * 1.0 2010-12-28 initial version */ 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. *

* 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 { // Test SunSort by sorting N random Strings 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++ ) { // keys of form A9999 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 ] ); } } // end main /** * Compare two Strings, case insensitively in reverse order. *

* Defines an alternate sort order for String. */ private static class DescendingStringComparator implements Comparator { /** * 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 ); } } }