commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Eric Jablow" <erjab...@praxiseng.com>
Subject RE: [lang] null-safe compareTo method available in collections
Date Mon, 20 Feb 2006 18:36:43 GMT

-----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


Mime
View raw message