com.g11ntoolkit.sorter
Class Sorter

java.lang.Object
  |
  +--com.g11ntoolkit.sorter.Sorter

public class Sorter
extends java.lang.Object

Defines a bunch of static methods for efficiently sorting arrays of Strings or other objects.

It also defines two interfaces that provide two different ways of comparing objects to be sorted.

Version:
2005/06/20

This class is based on the Sorter.java example in the Java Examples in a Nutshell, Second Edition by David Flanagan, published by O'Reilly in September, 2000.

Author:
Bill Rich, Wilandra Consulting LLC
Copyright © 2002-2005, Wilandra Consulting LLC. All rights reserved.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

See License Agreement.


Nested Class Summary
static interface Sorter.Comparable
          An alternative interface that can be used to order objects.
static interface Sorter.Comparer
          Defines the compare() method used to compare two objects.
 
Field Summary
private static Sorter.Comparer ascii_comparer
          An internal Comparer object (created with an anonymous class that compares two ASCII string objects.
private static Sorter.Comparer comparable_comparer
          An internal Comparer object.
private static java.util.logging.Logger log
          The log used for all messages from this class.
protected static java.util.ResourceBundle mrb
          Message Resource Bundle.
private static Sorter.Comparer token_comparer
          An internal Comparer object (created with an anonymous class that compares two Token objects.
protected static java.util.ResourceBundle vrb
          Variables Resource Bundle.
 
Constructor Summary
Sorter()
           
 
Method Summary
static void sort(java.lang.Object[] a)
          Sort an array of Token objects into ascending order.
static void sort(java.lang.Object[] a, int from, int to, boolean up, Sorter.Comparer c)
          Sort a portion of an array of arbitrary objects using the comparison defined by a Comparer object.
static void sort(java.lang.Object[] a, java.lang.Object[] b, int from, int to, boolean up, Sorter.Comparer c)
          Main sort routine.
static void sort(java.lang.Object[] a, Sorter.Comparer c)
          Sort an array of arbitrary objects into ascending order using the comparison defined by a Comparer object.
static void sort(Sorter.Comparable[] a)
          Sort an array of Comparable objects into ascending order.
static void sort(Sorter.Comparable[] a, int from, int to, boolean up)
          Sort a portion of an array of Comparable objects.
static void sort(Sorter.Comparable[] a, java.lang.Object[] b, int from, int to, boolean up)
          Sort a portion of an array of Comparable objects.
static void sort(java.lang.String[] a)
          Sort an array of strings into ascending order using the correct collation order for the default locale.
static void sort(java.lang.String[] a, int from, int to, boolean up, boolean ignorecase)
          Sort a portion of an array of strings into ascending or descending order using the collation order of the default locale.
static void sort(java.lang.String[] a, int from, int to, boolean up, boolean ignorecase, java.util.Locale locale)
          Sort a portion of an array of strings.
static void sortASCII(java.lang.String[] a)
          Sort an array of ASCII strings into ascending order.
static void sortASCII(java.lang.String[] a, int from, int to, boolean up)
          Sort a portion of an array of ASCII strings into ascending or descending order, depending on the argument up.
static void sortASCIIIgnoreCase(java.lang.String[] a)
          Sort an array of ASCII strings into ascending order ignoring case.
static void sortASCIIIgnoreCase(java.lang.String[] a, int from, int to, boolean up)
          Sort a portion of an array of ASCII strings into either ascending or descending order ignoring case.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

log

private static java.util.logging.Logger log
The log used for all messages from this class.


mrb

protected static java.util.ResourceBundle mrb
Message Resource Bundle.


vrb

protected static java.util.ResourceBundle vrb
Variables Resource Bundle.


ascii_comparer

private static Sorter.Comparer ascii_comparer
An internal Comparer object (created with an anonymous class that compares two ASCII string objects.

It is used in the sortASCII methods below.


token_comparer

private static Sorter.Comparer token_comparer
An internal Comparer object (created with an anonymous class that compares two Token objects.


comparable_comparer

private static Sorter.Comparer comparable_comparer
An internal Comparer object.

It is used to compare two Comparable objects. It is used by the sort methods below that take Comparable objects as arguments instead of arbitrary objects.

Constructor Detail

Sorter

public Sorter()
Method Detail

sortASCII

public static void sortASCII(java.lang.String[] a)
Sort an array of ASCII strings into ascending order.

Parameters:
a - a String array specifying the strings to be sorted

sortASCII

public static void sortASCII(java.lang.String[] a,
                             int from,
                             int to,
                             boolean up)
Sort a portion of an array of ASCII strings into ascending or descending order, depending on the argument up.

Parameters:
a - a String array containing the strings to be sorted
from - an int specifying the first element of the array to be sorted
to - an int specifying the last element of the array to be sorted
up - a boolean specifying whether to sort the elements in ascending (TRUE) order or not (FALSE)

sortASCIIIgnoreCase

public static void sortASCIIIgnoreCase(java.lang.String[] a)
Sort an array of ASCII strings into ascending order ignoring case.

Parameters:
a - a String array specifying the strings to be sorted

sortASCIIIgnoreCase

public static void sortASCIIIgnoreCase(java.lang.String[] a,
                                       int from,
                                       int to,
                                       boolean up)
Sort a portion of an array of ASCII strings into either ascending or descending order ignoring case.

Parameters:
a - a String array containing the strings to be sorted
from - an int specifying the first element of the array to be sorted
to - an int specifying the last element of the array to be sorted
up - a boolean specifying whether to sort the elements in ascending (TRUE) order or not (FALSE)

sort

public static void sort(java.lang.String[] a)
Sort an array of strings into ascending order using the correct collation order for the default locale.

Parameters:
a - a String array specifying the strings to be sorted

sort

public static void sort(java.lang.String[] a,
                        int from,
                        int to,
                        boolean up,
                        boolean ignorecase)
Sort a portion of an array of strings into ascending or descending order using the collation order of the default locale.

If the ignorecase parameter is true, treat all characters as lowercase.

Parameters:
a - a String array containing the strings to be sorted
from - an int specifying the first element of the array to be sorted
to - an int specifying the last element of the array to be sorted
up - a boolean specifying whether to sort the elements in ascending (TRUE) order or not (FALSE)
ignorecase - a boolean specifying whether to ignore the case of the characters (TRUE) or not (FALSE)

sort

public static void sort(java.lang.String[] a,
                        int from,
                        int to,
                        boolean up,
                        boolean ignorecase,
                        java.util.Locale locale)
Sort a portion of an array of strings.

Use the collation order of the specified locale. If the ignorecase parameter is true, treat all characters as lowercase.

Parameters:
a - a String array containing the strings to be sorted
from - an int specifying the first element of the array to be sorted
to - an int specifying the last element of the array to be sorted
up - a boolean specifying whether to sort the elements in ascending (TRUE) order or not (FALSE)
ignorecase - a boolean specifying whether to ignore the case of the characters (TRUE) or not (FALSE)
locale - a Locale specifying the locale to use for the proper ordering of the characters in the strings

sort

public static void sort(Sorter.Comparable[] a)
Sort an array of Comparable objects into ascending order.

Parameters:
a - a Comparable object array specifying the objects to sort

sort

public static void sort(Sorter.Comparable[] a,
                        int from,
                        int to,
                        boolean up)
Sort a portion of an array of Comparable objects.

Parameters:
a - a Comparable object array specifying the objects to sort
from - an int specifying the first element of the array to be sorted
to - an int specifying the last element of the array to be sorted
up - a boolean specifying whether to sort the elements in ascending (TRUE) order or not (FALSE)

sort

public static void sort(Sorter.Comparable[] a,
                        java.lang.Object[] b,
                        int from,
                        int to,
                        boolean up)
Sort a portion of an array of Comparable objects.

Re-arrange the array in exactly the same was in another array.

Parameters:
a - a Comparable object array specifying the objects to sort
b - an object array specifying array to hold the results of the sort
from - an int specifying the first element of the array to be sorted
to - an int specifying the last element of the array to be sorted
up - a boolean specifying whether to sort the elements in ascending (TRUE) order or not (FALSE)

sort

public static void sort(java.lang.Object[] a)
Sort an array of Token objects into ascending order.

Parameters:
a - a Token object array specifying the Token objects to sort.

sort

public static void sort(java.lang.Object[] a,
                        Sorter.Comparer c)
Sort an array of arbitrary objects into ascending order using the comparison defined by a Comparer object.

Parameters:
a - an Object array specifying the objects to sort
c - a Comparer specifying the comparison method to use

sort

public static void sort(java.lang.Object[] a,
                        int from,
                        int to,
                        boolean up,
                        Sorter.Comparer c)
Sort a portion of an array of arbitrary objects using the comparison defined by a Comparer object.

Parameters:
a - an Object array specifying the objects to sort
from - an int specifying the first element of the array to be sorted
to - an int specifying the last element of the array to be sorted
up - a boolean specifying whether to sort the elements in ascending (TRUE) order or not (FALSE)
c - a Comparer specifying the comparison method to use

sort

public static void sort(java.lang.Object[] a,
                        java.lang.Object[] b,
                        int from,
                        int to,
                        boolean up,
                        Sorter.Comparer c)
Main sort routine.

Performs a quicksort on the elements of an array between the elements specified by the from and to arguments. The c argument is used to perform comparisons between elements of the array. The elements of the array are reordered and placed in the array specified by the b argument.

Parameters:
a - an Object array specifying the objects to sort
b - an Object array specifying the array to hold the sorted objects
from - an int specifying the first element of the array to be sorted
to - an int specifying the last element of the array to be sorted
up - a boolean specifying whether to sort the elements in ascending (TRUE) order or not (FALSE)
c - a Comparer specifying the comparison method to use