Java Reference
In-Depth Information
Example 2•9: Sorter.java (continued)
* if (this == other) return 0
* if (this < other) return < 0
**/
public int compareTo(Object other);
}
/**
* This is an internal Comparer object (created with an anonymous class)
* that compares two ASCII strings.
* It is used in the sortAscii methods below.
**/
private static Comparer ascii_comparer = new Comparer() {
public int compare(Object a, Object b) {
return ((String)a).compareTo((String)b);
}
};
/**
* This is another 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
**/
private static Comparer comparable_comparer = new Comparer() {
public int compare(Object a, Object b) {
return ((Comparable)a).compareTo(b);
}
};
/** Sort an array of ASCII strings into ascending order */
public static void sortAscii(String[] a) {
// Note use of the ascii_comparer object
sort(a, null, 0, a.length-1, true, ascii_comparer);
}
/**
* Sort a portion of an array of ASCII strings into ascending or descending
* order, depending on the argument up
**/
public static void sortAscii(String[] a, int from, int to, boolean up) {
// Note use of the ascii_comparer object
sort(a, null, from, to, up, ascii_comparer);
}
/** Sort an array of ASCII strings into ascending order, ignoring case */
public static void sortAsciiIgnoreCase(String[] a) {
sortAsciiIgnoreCase(a, 0, a.length-1, true);
}
/**
* Sort an portion of an array of ASCII strings, ignoring case. Sort into
* ascending order if up is true, otherwise sort into descending order.
**/
public static void sortAsciiIgnoreCase(String[] a, int from, int to,
boolean up) {
if ((a == null) || (a.length < 2)) return;
// Create a secondary array of strings that contains lowercase versions
// of all the specified strings.
String b[] = new String[a.length];
Search WWH ::




Custom Search