Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > verbose sort

Reply
Thread Tools

verbose sort

 
 
bob smith
Guest
Posts: n/a
 
      08-02-2012
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?

 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      08-02-2012
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
http://www.velocityreviews.com/forums/(E-Mail Removed)d
 
Reply With Quote
 
 
 
 
markspace
Guest
Posts: n/a
 
      08-02-2012
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 );


 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      08-02-2012
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
(E-Mail Removed)d
 
Reply With Quote
 
markspace
Guest
Posts: n/a
 
      08-02-2012
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.



 
Reply With Quote
 
Daniel Pitts
Guest
Posts: n/a
 
      08-02-2012
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.
 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      08-02-2012
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
 
Reply With Quote
 
bob smith
Guest
Posts: n/a
 
      08-02-2012
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.
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      08-03-2012
On Thu, 2 Aug 2012 08:37:07 -0700 (PDT), bob smith
<(E-Mail Removed)> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: verbose sort Lew Java 0 08-04-2012 06:41 PM
Re: verbose sort Daniel Pitts Java 0 08-04-2012 06:41 PM
verbose sort bob smith Java 8 08-03-2012 06:54 PM
Re: verbose sort Volker Borchert Java 0 08-03-2012 06:54 PM
Re: verbose sort Eric Sosman Java 0 08-02-2012 07:12 PM



Advertisments