Re: [guava] ComparisonChain comparing a list of custom objects

434 views
Skip to first unread message

Louis Wasserman

unread,
Mar 15, 2013, 1:25:10 AM3/15/13
to Sonny Heer, guava-discuss
Perhaps you mean Ordering.natural().lexicographical().nullsFirst()?



On Thu, Mar 14, 2013 at 7:34 PM, Sonny Heer <sonn...@gmail.com> wrote:

To me it seems the following should handle nulls for me (assuming left/right are null):

//                .compare(
//                        unstructuredAnnotations,
//                        rhs.unstructuredAnnotations,
//                        Ordering.<UnstructuredAnnotation> natural()
//                                .nullsFirst().lexicographical())

I get a NPE on line 42 of the LexicographicalOrdering Class.  Am I doing something wrong?

Thanks,
-Sonny

--
--
guava-...@googlegroups.com
Project site: http://guava-libraries.googlecode.com
This group: http://groups.google.com/group/guava-discuss
 
This list is for general discussion.
To report an issue: http://code.google.com/p/guava-libraries/issues/entry
To get help: http://stackoverflow.com/questions/ask (use the tag "guava")
 
---
You received this message because you are subscribed to the Google Groups "guava-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to guava-discus...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Torbjorn Gannholm

unread,
Mar 15, 2013, 5:05:27 AM3/15/13
to Louis Wasserman, Sonny Heer, guava-discuss



On Fri, Mar 15, 2013 at 6:25 AM, Louis Wasserman <wasserm...@gmail.com> wrote:
Perhaps you mean Ordering.natural().lexicographical().nullsFirst()?

Ouch, that seems like a flaw in this kind of interface.

Kevin Bourrillion

unread,
Mar 15, 2013, 10:28:49 AM3/15/13
to Torbjorn Gannholm, Louis Wasserman, Sonny Heer, guava-discuss
Yes, I've always felt it was the biggest flaw with Ordering.  One way puts null lists first, the other way puts null elements first, and it takes some puzzling to keep track of which is which. It usually helps to read chains of Ordering calls backwards.  When comparing two lists with the OP's code, first we break it down lexicographically, then in the course of comparing an element we sort nulls first, then when both are non-null we settle it with Ordering.natural().

This should be on stack overflow!

Kevin Bourrillion | Java Librarian | Google, Inc. | kev...@google.com

Sonny Heer

unread,
Mar 15, 2013, 11:47:51 AM3/15/13
to guava-...@googlegroups.com, Torbjorn Gannholm, Louis Wasserman, Sonny Heer
So is there I pull this off where it will handle nulls in a list with custom objects (or even non custom objects, String)?  In the case of nonnulls would be nice for it drill down to the compare of the list object (UnstructuredAnnotation object in this case).  I can post on stackoverflow if needed...

Louis Wasserman

unread,
Mar 15, 2013, 12:17:05 PM3/15/13
to Sonny Heer, guava-...@googlegroups.com, Torbjorn Gannholm, Louis Wasserman
I don't think we could, or should, change the semantics of Ordering now.  The two orderings of "nullsFirst" and "lexicographical" do genuinely mean different things, and it's not at all clear to me that users always want one and not the other.
Louis Wasserman

Kevin Bourrillion

unread,
Mar 15, 2013, 1:11:52 PM3/15/13
to Louis Wasserman, Sonny Heer, guava-...@googlegroups.com, Torbjorn Gannholm, Louis Wasserman
No, of course not.  You can even have Ordering.natural().nullsFirst().lexicographical().nullsLast()!  It has a well-defined meaning that is sometimes even what you need.  The only fix we have for this problem is more and better documentation.

Kevin Bourrillion

unread,
Mar 15, 2013, 1:18:16 PM3/15/13
to Louis Wasserman, Sonny Heer, guava-...@googlegroups.com, Torbjorn Gannholm, Louis Wasserman
Reply all
Reply to author
Forward
0 new messages