verbose sort

Discussion in 'Java' started by bob smith, Aug 2, 2012.

  1. bob smith

    bob smith Guest

    I have some code that sorts a list like so:

    Vector<String> my_list = new Vector<String>();


    Comparator<String> c = new Comparator<String>() {
    @Override
    public int compare(String object1, String object2) {
    if (object1 == null)
    return -1;
    if (object2 == null)
    return 1;
    object1 = object1.toLowerCase();
    object2 = object2.toLowerCase();
    return object1.compareTo(object2);
    };
    };

    Collections.sort(my_list, c);


    This seems like a lot of code for such a common operation. Is there a more succinct way of doing this?
    bob smith, Aug 2, 2012
    #1
    1. Advertising

  2. bob smith

    Eric Sosman Guest

    On 8/2/2012 11:37 AM, bob smith wrote:
    > I have some code that sorts a list like so:
    >
    > Vector<String> my_list = new Vector<String>();
    >
    >
    > Comparator<String> c = new Comparator<String>() {
    > @Override
    > public int compare(String object1, String object2) {
    > if (object1 == null)
    > return -1;
    > if (object2 == null)
    > return 1;
    > object1 = object1.toLowerCase();
    > object2 = object2.toLowerCase();
    > return object1.compareTo(object2);
    > };
    > };
    >
    > Collections.sort(my_list, c);
    >
    >
    > This seems like a lot of code for such a common operation. Is there a more succinct way of doing this?


    Consider using compareToIgnoreCase(). Also, think about what
    happens when two null's are compared: You should return zero rather
    than declaring one of them "less than" the other, because otherwise
    your comparator is inconsistent (you can have A<B, B<C, but C<A).

    public int compare(String s1, String s2) {
    if (s1 == null)
    return s2 == null ? 0 : -1;
    return s2 == null ? +1 : s1.compareToIgnoreCase(s2);
    }

    --
    Eric Sosman
    d
    Eric Sosman, Aug 2, 2012
    #2
    1. Advertising

  3. bob smith

    markspace Guest

    On 8/2/2012 8:37 AM, bob smith wrote:
    > I have some code that sorts a list like so:
    >
    > Vector<String> my_list = new Vector<String>();
    >
    >
    > Comparator<String> c = new Comparator<String>() {
    > @Override
    > public int compare(String object1, String object2) {
    > if (object1 == null)
    > return -1;
    > if (object2 == null)
    > return 1;
    > object1 = object1.toLowerCase();
    > object2 = object2.toLowerCase();
    > return object1.compareTo(object2);
    > };
    > };
    >
    > Collections.sort(my_list, c);
    >
    >
    > This seems like a lot of code for such a common operation.
    > Is there a more succinct way of doing this?
    >



    Collections.sort( my_list, String.CASE_INSENSITIVE_ORDER );
    markspace, Aug 2, 2012
    #3
  4. bob smith

    Eric Sosman Guest

    On 8/2/2012 1:19 PM, markspace wrote:
    > On 8/2/2012 8:37 AM, bob smith wrote:
    >> I have some code that sorts a list like so:
    >>
    >> Vector<String> my_list = new Vector<String>();
    >>
    >>
    >> Comparator<String> c = new Comparator<String>() {
    >> @Override
    >> public int compare(String object1, String object2) {
    >> if (object1 == null)
    >> return -1;
    >> if (object2 == null)
    >> return 1;
    >> object1 = object1.toLowerCase();
    >> object2 = object2.toLowerCase();
    >> return object1.compareTo(object2);
    >> };
    >> };
    >>
    >> Collections.sort(my_list, c);
    >>
    >>
    >> This seems like a lot of code for such a common operation.
    > > Is there a more succinct way of doing this?
    >>

    >
    >
    > Collections.sort( my_list, String.CASE_INSENSITIVE_ORDER );


    Throws NullPointerException if the list has any nulls.

    --
    Eric Sosman
    d
    Eric Sosman, Aug 2, 2012
    #4
  5. bob smith

    markspace Guest

    On 8/2/2012 10:59 AM, Eric Sosman wrote:

    > On 8/2/2012 1:19 PM, markspace wrote:
    >> Collections.sort( my_list, String.CASE_INSENSITIVE_ORDER );


    >
    > Throws NullPointerException if the list has any nulls.
    >



    That's unfortunate. I thought there were more "pre-made" Comparators,
    but couldn't find any. That too bad too, some wrappers would handle a
    large number of situations, including null checks.

    I really thought there were more Comparators in the API, I might just be
    missing them.
    markspace, Aug 2, 2012
    #5
  6. bob smith

    Daniel Pitts Guest

    On 8/2/12 11:14 AM, markspace wrote:
    > On 8/2/2012 10:59 AM, Eric Sosman wrote:
    >
    >> On 8/2/2012 1:19 PM, markspace wrote:
    >>> Collections.sort( my_list, String.CASE_INSENSITIVE_ORDER );

    >
    >>
    >> Throws NullPointerException if the list has any nulls.
    >>

    >
    >
    > That's unfortunate. I thought there were more "pre-made" Comparators,
    > but couldn't find any. That too bad too, some wrappers would handle a
    > large number of situations, including null checks.
    >
    > I really thought there were more Comparators in the API, I might just be
    > missing them.
    >
    >
    >

    I believe Apache Commons has a bunch of useful generic Comparator
    implementations.
    Daniel Pitts, Aug 2, 2012
    #6
  7. bob smith

    Lew Guest

    bob smith wrote:
    > I have some code that sorts a list like so:
    >
    > Vector<String> my_list = new Vector<String>();
    >
    > Comparator<String> c = new Comparator<String>() {
    > @Override
    > public int compare(String object1, String object2) {
    > if (object1 == null)
    > return -1;
    >
    > if (object2 == null)
    > return 1;
    >
    > object1 = object1.toLowerCase();
    > object2 = object2.toLowerCase();
    >
    > return object1.compareTo(object2);
    > };
    > };
    >
    > Collections.sort(my_list, c);
    >
    > This seems like a lot of code for such a common operation. Is there a more succinct way of doing this?


    Others have shown ways to shorten this, but I'm curious.

    "Seems" - such a duck-and-cover word. You made an assessment.
    Based on what criteria?

    What is "a lot"?

    You could write a cover method.

    Apache Commons might have a utility class for that.

    --
    Lew
    Lew, Aug 2, 2012
    #7
  8. bob smith

    bob smith Guest

    On Thursday, August 2, 2012 12:19:50 PM UTC-5, markspace wrote:
    > On 8/2/2012 8:37 AM, bob smith wrote:
    >
    > > I have some code that sorts a list like so:

    >
    > >

    >
    > > Vector<String> my_list = new Vector<String>();

    >
    > >

    >
    > >

    >
    > > Comparator<String> c = new Comparator<String>() {

    >
    > > @Override

    >
    > > public int compare(String object1, String object2) {

    >
    > > if (object1 == null)

    >
    > > return -1;

    >
    > > if (object2 == null)

    >
    > > return 1;

    >
    > > object1 = object1.toLowerCase();

    >
    > > object2 = object2.toLowerCase();

    >
    > > return object1.compareTo(object2);

    >
    > > };

    >
    > > };

    >
    > >

    >
    > > Collections.sort(my_list, c);

    >
    > >

    >
    > >

    >
    > > This seems like a lot of code for such a common operation.

    >
    > > Is there a more succinct way of doing this?

    >
    > >

    >
    >
    >
    >
    >
    > Collections.sort( my_list, String.CASE_INSENSITIVE_ORDER );


    Very nice, thanks.
    bob smith, Aug 2, 2012
    #8
  9. bob smith

    Roedy Green Guest

    On Thu, 2 Aug 2012 08:37:07 -0700 (PDT), bob smith
    <> wrote, quoted or indirectly quoted someone
    who said :

    >
    >This seems like a lot of code for such a common operation. Is there a more succinct way of doing this?


    see http://mindprod.com/applet/comparatorcutter.html
    --
    Roedy Green Canadian Mind Products
    http://mindprod.com
    The greatest shortcoming of the human race is our inability to understand the exponential function.
    ~ Dr. Albert A. Bartlett (born: 1923-03-21 age: 89)
    http://www.youtube.com/watch?v=F-QA2rkpBSY
    Roedy Green, Aug 3, 2012
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Eric Sosman

    Re: verbose sort

    Eric Sosman, Aug 2, 2012, in forum: Java
    Replies:
    0
    Views:
    171
    Eric Sosman
    Aug 2, 2012
  2. bob smith

    verbose sort

    bob smith, Aug 2, 2012, in forum: Java
    Replies:
    8
    Views:
    246
    Roedy Green
    Aug 3, 2012
  3. Volker Borchert

    Re: verbose sort

    Volker Borchert, Aug 3, 2012, in forum: Java
    Replies:
    0
    Views:
    170
    Volker Borchert
    Aug 3, 2012
  4. Daniel Pitts

    Re: verbose sort

    Daniel Pitts, Aug 4, 2012, in forum: Java
    Replies:
    0
    Views:
    124
    Daniel Pitts
    Aug 4, 2012
  5. Lew

    Re: verbose sort

    Lew, Aug 4, 2012, in forum: Java
    Replies:
    0
    Views:
    125
Loading...

Share This Page