Class AlphaNumericComparator
java.lang.Object
journeymap.api.v2.common.util.AlphaNumericComparator
- All Implemented Interfaces:
Comparator<String>
An alphanumeric comparator for comparing strings in a human readable format. It uses a combination numeric and alphabetic comparisons to compare two strings. This class uses standard Java classes, independent of 3rd party libraries.
For given list of strings:
The regular lexicographical sort e.g.file-01.doc file-2.doc file-03.doc
Collections.sort(java.util.List) will result in a sorted list of:
But using this class, the result will be a more human readable and organizable sorted list of:file-01.doc file-03.doc file-2.doc
file-01.doc file-2.doc file-03.doc
Additionally this comparator uses Collator class to correctly sort
strings containing special Unicode characters such as Umlauts and other similar letters of
alphabet in different languages, such as: å, è, ü, ö, ø, or ý.
-
Constructor Summary
ConstructorsConstructorDescriptionDefault constructor, uses the default Locale and default collator strength.AlphaNumericComparator(Locale locale) Constructor using the providedLocaleand default collator strength.AlphaNumericComparator(Locale locale, int strength) Constructor with givenLocaleand collator strength value. -
Method Summary
Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface Comparator
equals, reversed, thenComparing, thenComparing, thenComparing, thenComparingDouble, thenComparingInt, thenComparingLong
-
Constructor Details
-
AlphaNumericComparator
public AlphaNumericComparator()Default constructor, uses the default Locale and default collator strength. -
AlphaNumericComparator
Constructor using the providedLocaleand default collator strength.- Parameters:
locale- DesiredLocale
-
AlphaNumericComparator
Constructor with givenLocaleand collator strength value.- Parameters:
locale- DesiredLocalestrength- Collator strength value, any of:Collator.PRIMARY,Collator.SECONDARY,Collator.TERTIARY, orCollator.IDENTICAL- See Also:
-
-
Method Details
-
compare
Compares two givenStringparameters. Both string parameters will be trimmed before comparison.If either parameter is
nullor empty after trim:-1when only the first parameter is null/empty,1when only the second parameter is null/empty,0when both are null/empty.
- Specified by:
comparein interfaceComparator<String>
-