public class String_Vector_Comparator extends Object implements Comparator
It is particularly useful, for example, in sorting a set of results from a Database query. Use an instance of this class as the Comparator in the Collections.sort(List, Comparator) static method, which actually does the sorting. The Objects to be compared are assumed to be Lists. The Objects at the specified index of each List are converted to Strings. If these Strings can be parsed as Doubles then these are compared numerically. Otherwise the Strings are are compared lexicographically.
Modifier and Type | Field and Description |
---|---|
static String |
ID
Class identification name with source code version and date.
|
Constructor and Description |
---|
String_Vector_Comparator()
Construct a String_Vector_Comparator that will
compare index zero of the Lists. |
String_Vector_Comparator(int index)
Construct a String_Vector_Comparator using a sepecified index for the
Lists to be
compared . |
Modifier and Type | Method and Description |
---|---|
int |
compare(Object list_0,
Object list_1)
Compare two Lists.
|
int |
Index()
Get the List index to be
compared . |
String_Vector_Comparator |
Index(int index)
Set the List index to be
compared . |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
equals
public static final String ID
public String_Vector_Comparator(int index)
compared
.
index
- The List index to be compared.public String_Vector_Comparator()
compare
index zero of the Lists.public String_Vector_Comparator Index(int index)
compared
.
index
- The List index to be compared.IllegalArgumentException
- If the index is negative.public int Index()
compared
.
Index(int)
public int compare(Object list_0, Object list_1)
The objects at each List's Index
entry are first
checked for being null. If both are null, 0 is returned; if only
the first is null, -1 is returned; if only the second is null, 1
is returned.
Non-null list entries are converted to a Strings. If these Strings can be converted to Doubles, then they are compared numerically using Double.compareTo. Otherwise they are compared lexicographically using String compareTo.
compare
in interface Comparator
list_0
- The first List for comparison.list_1
- The second List for comparison.Index
entry is less than, equal to,
or greater than the second List Index
entry.IllegalArgumentException
- If the arguments are not Lists.ArrayIndexOutOfBoundsException
- If the arguments do not
have at least Index
+ 1 entries.