public final class NaturalSortComparator extends Object implements Comparator
This is a Comparator that explicitly uses plain Object as input and utilizes the
Object.toString() to create String objects for sorting.
The algorithm used here in this implementation is optimized to avoid unnecessary check-cycles.
| Constructor and Description |
|---|
NaturalSortComparator() |
| Modifier and Type | Method and Description |
|---|---|
int |
compare(Object o1,
Object o2) |
static NaturalSortComparator |
getInstance() |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitequalspublic final int compare(Object o1, Object o2)
This implementation sorts arbitrary Objects in natural order. Only prerequisite is a properly implemented
Object.toString() method. Also helpful are proper implementations of
Object.equals(java.lang.Object) and Object.hashCode(), though neither of these two methods are used
directly by this Comparator implementation. However, they might be used indirectly.
This is an example of what a sorted list of Strings would look like after being sorted with this Comparator:
{
"1-04", "1-4", "1-40", "10-40", "Alice", "Bob", "Charly",
"a6-b6", "h2-i7", "item01", "item02", "item02a", "item2", "item3",
"item00004", "item4", "item 4 else", "item05", "item 5", "item 5",
"item 5 something", "item 6", "item 8", "item128", "item128a",
"item255", "item256", "item04096", "item04096 test 1",
"item04096 test 2", "item04096 test 2a", "item04096 test 2b",
"item04096 test 3", "item04096 test 3a", "item04096 test 3b",
"x2-y08", "z3-f6"
}
compare in interface Comparatoro1 - The first object to be compared.o2 - The second object to be compared.NullPointerException - if an argument (i.e. o1 or o1) is nullpublic static final NaturalSortComparator getInstance()
Copyright © 2016. All Rights Reserved.