Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #16962 > unrolled thread

verbose sort

Started bybob smith <bob@coolfone.comze.com>
First post2012-08-02 08:37 -0700
Last post2012-08-03 01:37 -0700
Articles 9 — 6 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  verbose sort bob smith <bob@coolfone.comze.com> - 2012-08-02 08:37 -0700
    Re: verbose sort Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-08-02 12:28 -0400
    Re: verbose sort markspace <-@.> - 2012-08-02 10:19 -0700
      Re: verbose sort Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-08-02 13:59 -0400
        Re: verbose sort markspace <-@.> - 2012-08-02 11:14 -0700
          Re: verbose sort Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2012-08-02 13:38 -0700
      Re: verbose sort bob smith <bob@coolfone.comze.com> - 2012-08-02 15:19 -0700
    Re: verbose sort Lew <lewbloch@gmail.com> - 2012-08-02 14:15 -0700
    Re: verbose sort Roedy Green <see_website@mindprod.com.invalid> - 2012-08-03 01:37 -0700

#16962 — verbose sort

Frombob smith <bob@coolfone.comze.com>
Date2012-08-02 08:37 -0700
Subjectverbose sort
Message-ID<ad14b658-e1c5-483a-a447-802725d731ff@googlegroups.com>
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?

[toc] | [next] | [standalone]


#16963

FromEric Sosman <esosman@ieee-dot-org.invalid>
Date2012-08-02 12:28 -0400
Message-ID<jve9sd$o2p$1@dont-email.me>
In reply to#16962
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

[toc] | [prev] | [next] | [standalone]


#16967

Frommarkspace <-@.>
Date2012-08-02 10:19 -0700
Message-ID<jvecro$bcn$1@dont-email.me>
In reply to#16962
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 );

[toc] | [prev] | [next] | [standalone]


#16968

FromEric Sosman <esosman@ieee-dot-org.invalid>
Date2012-08-02 13:59 -0400
Message-ID<jvef6e$q07$1@dont-email.me>
In reply to#16967
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

[toc] | [prev] | [next] | [standalone]


#16969

Frommarkspace <-@.>
Date2012-08-02 11:14 -0700
Message-ID<jveg21$vgv$1@dont-email.me>
In reply to#16968
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.


[toc] | [prev] | [next] | [standalone]


#17002

FromDaniel Pitts <newsgroup.nospam@virtualinfinity.net>
Date2012-08-02 13:38 -0700
Message-ID<nzBSr.2213$RL6.658@newsfe13.iad>
In reply to#16969
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.

[toc] | [prev] | [next] | [standalone]


#17009

Frombob smith <bob@coolfone.comze.com>
Date2012-08-02 15:19 -0700
Message-ID<5ce05de2-d803-4f30-aecb-bf02a5211f7e@googlegroups.com>
In reply to#16967
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.

[toc] | [prev] | [next] | [standalone]


#17005

FromLew <lewbloch@gmail.com>
Date2012-08-02 14:15 -0700
Message-ID<e8f2505e-7079-49a7-b0dd-22c456524fa8@googlegroups.com>
In reply to#16962
 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

[toc] | [prev] | [next] | [standalone]


#17033

FromRoedy Green <see_website@mindprod.com.invalid>
Date2012-08-03 01:37 -0700
Message-ID<ie3n1858ont34fo37v1el6d92atc9g8fma@4ax.com>
In reply to#16962
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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web