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?
|
|
0
|
|
|
|
Reply
|
bob3904 (239)
|
8/2/2012 3:37:07 PM |
|
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
esosman@ieee-dot-org.invalid
|
|
0
|
|
|
|
Reply
|
esosman2 (2945)
|
8/2/2012 4:28:58 PM
|
|
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 );
|
|
0
|
|
|
|
Reply
|
markspace
|
8/2/2012 5:19:50 PM
|
|
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
esosman@ieee-dot-org.invalid
|
|
0
|
|
|
|
Reply
|
esosman2 (2945)
|
8/2/2012 5:59:36 PM
|
|
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.
|
|
0
|
|
|
|
Reply
|
markspace
|
8/2/2012 6:14:23 PM
|
|
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.
|
|
0
|
|
|
|
Reply
|
newsgroup.nospam (532)
|
8/2/2012 8:38:39 PM
|
|
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
|
|
0
|
|
|
|
Reply
|
lewbloch (1312)
|
8/2/2012 9:15:22 PM
|
|
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.
|
|
0
|
|
|
|
Reply
|
bob3904 (239)
|
8/2/2012 10:19:14 PM
|
|
On Thu, 2 Aug 2012 08:37:07 -0700 (PDT), bob smith
<bob@coolfone.comze.com> 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
|
|
0
|
|
|
|
Reply
|
see_website (4858)
|
8/3/2012 8:37:51 AM
|
|
|
8 Replies
25 Views
(page loaded in 0.101 seconds)
|