You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Eric Jablow <er...@praxiseng.com> on 2006/02/20 19:36:43 UTC

RE: [lang] null-safe compareTo method available in collections

-----Original Message-----
From: news [mailto:news@sea.gmane.org] On Behalf Of Geoffrey De Smet
Sent: Monday, February 20, 2006 6:05 AM
> Is there a null-safe compare method?
> I need to compare 2 strings:
> - both null => equal
> - one null => that one is smaller
> - neither null => a.compareTo(b)

> Else I 'd propose something like:
> ObjectUtils.compare(Comparable a, Comparable b)

You can use the Commons Collections library and its NullComparator
class.  In fact, its ComparatorUtils static class provides what you
need.

Comparator comp =
 
ComparatorUtils.nullLowComparator(ComparatorUtils.NATURAL_COMPARATOR);
    // Or nullHighComparator if you need that instead.
int order = comp.compare(a, b);

If you use JDK 5.0, look at the 5.0 port of collections on SourceForge.

Respectfully,
Eric Jablow




---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org