verbose sort

B

bob smith

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

Eric Sosman

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);
}
 
M

markspace

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 );
 
M

markspace

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

Daniel Pitts

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

Lew

bob said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top