public class HumaneStringComparator extends Object implements Comparator<String>
A comparator for Strings that imposes a more "human" order on Strings. Modeled on the way the Mac OS X Finder orders file names, it breaks the Strings into segments of non-numbers and numbers (e.g. Foo123Bar into 'Foo', 123, 'Bar') and compares the segments from each String one at a time.
The following rules are applied when comparing segments:
If two Strings are identical after analyzing them in a segment-at-time manner then lhs.compareTo(rhs) will be invoked to ensure that the result provided by the comparator is reflexive (i.e. compare(x,y) = -compare(y,x)) and agrees with String.equals() in all cases.
As an example, the following list of Strings is presented in the order in which the comparator will sort them:
| Modifier and Type | Field and Description |
|---|---|
static HumaneStringComparator |
DEFAULT
A default instance of the comparator that can be used without instantiating
a new copy every time one is needed.
|
| Constructor and Description |
|---|
HumaneStringComparator() |
| Modifier and Type | Method and Description |
|---|---|
int |
compare(String lhs,
String rhs)
The implementation of the Comparable interface method that compares two
Strings.
|
protected int |
compareNumberSegments(String lhs,
String rhs)
Converts the two Strings to doubles and then compares then numerically
by invoking Double.compareTo()
|
protected int |
compareStringSegments(String lhs,
String rhs)
Compares the left hand String to the right hand String case-insensitively
by invoking lhs.compareToIgnoreCase(rhs).
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitequalspublic static final HumaneStringComparator DEFAULT
public int compare(String lhs, String rhs)
compare in interface Comparator<String>lhs - the first of two Strings to comparerhs - the second of two Strings to compareprotected int compareNumberSegments(String lhs, String rhs)
Copyright © 2008-2014 Red Hat, Inc.. All Rights Reserved.