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


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

verbose sort

Started by"bob smith" <bob.smith@1:261/38.remove-s5y-this>
First post2012-08-02 19:12 +0000
Last post2012-08-03 18:54 +0000
Articles 8 — 7 participants

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


Contents

  verbose sort "bob smith" <bob.smith@1:261/38.remove-s5y-this> - 2012-08-02 19:12 +0000
    Re: verbose sort "markspace" <markspace@1:261/38.remove-s5y-this> - 2012-08-02 19:12 +0000
      Re: verbose sort "Eric Sosman" <eric.sosman@1:261/38.remove-s5y-this> - 2012-08-02 19:12 +0000
        Re: verbose sort "markspace" <markspace@1:261/38.remove-s5y-this> - 2012-08-02 19:12 +0000
          Re: verbose sort "Daniel Pitts" <daniel.pitts@1:261/38.remove-yy0-this> - 2012-08-03 18:54 +0000
      Re: verbose sort "bob smith" <bob.smith@1:261/38.remove-yy0-this> - 2012-08-03 18:54 +0000
    Re: verbose sort "Lew" <lew@1:261/38.remove-yy0-this> - 2012-08-03 18:54 +0000
    Re: verbose sort "Roedy Green" <roedy.green@1:261/38.remove-yy0-this> - 2012-08-03 18:54 +0000

#16994 — verbose sort

From"bob smith" <bob.smith@1:261/38.remove-s5y-this>
Date2012-08-02 19:12 +0000
Subjectverbose sort
Message-ID<501AC32E.55954.calajapr@time.synchro.net>
From: bob smith <bob@coolfone.comze.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?

--- BBBS/Li6 v4.10 Dada-1
 * Origin: Prism bbs (1:261/38)
--- Synchronet 3.16a-Win32 NewsLink 1.98
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [next] | [standalone]


#16996

From"markspace" <markspace@1:261/38.remove-s5y-this>
Date2012-08-02 19:12 +0000
Message-ID<501AC32E.55957.calajapr@time.synchro.net>
In reply to#16994
  To: bob smith
From: markspace <-@.>

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

--- BBBS/Li6 v4.10 Dada-1
 * Origin: Prism bbs (1:261/38)
--- Synchronet 3.16a-Win32 NewsLink 1.98
Time Warp of the Future BBS - telnet://time.synchro.net:24

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


#16997

From"Eric Sosman" <eric.sosman@1:261/38.remove-s5y-this>
Date2012-08-02 19:12 +0000
Message-ID<501ACE76.55958.calajapr@time.synchro.net>
In reply to#16996
  To: markspace
From: Eric Sosman <esosman@ieee-dot-org.invalid>

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

--- BBBS/Li6 v4.10 Dada-1
 * Origin: Prism bbs (1:261/38)
--- Synchronet 3.16a-Win32 NewsLink 1.98
Time Warp of the Future BBS - telnet://time.synchro.net:24

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


#16998

From"markspace" <markspace@1:261/38.remove-s5y-this>
Date2012-08-02 19:12 +0000
Message-ID<501ACE77.55959.calajapr@time.synchro.net>
In reply to#16997
  To: Eric Sosman
From: markspace <-@.>

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.

--- BBBS/Li6 v4.10 Dada-1
 * Origin: Prism bbs (1:261/38)
--- Synchronet 3.16a-Win32 NewsLink 1.98
Time Warp of the Future BBS - telnet://time.synchro.net:24

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


#17060

From"Daniel Pitts" <daniel.pitts@1:261/38.remove-yy0-this>
Date2012-08-03 18:54 +0000
Message-ID<501C1563.56019.calajapr@time.synchro.net>
In reply to#16998
  To: markspace
From: Daniel Pitts <newsgroup.nospam@virtualinfinity.net>

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.

--- BBBS/Li6 v4.10 Dada-1
 * Origin: Prism bbs (1:261/38)
--- Synchronet 3.16a-Win32 NewsLink 1.98
Time Warp of the Future BBS - telnet://time.synchro.net:24

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


#17067

From"bob smith" <bob.smith@1:261/38.remove-yy0-this>
Date2012-08-03 18:54 +0000
Message-ID<501C1565.56026.calajapr@time.synchro.net>
In reply to#16996
  To: markspace
From: bob smith <bob@coolfone.comze.com>

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.

--- BBBS/Li6 v4.10 Dada-1
 * Origin: Prism bbs (1:261/38)
--- Synchronet 3.16a-Win32 NewsLink 1.98
Time Warp of the Future BBS - telnet://time.synchro.net:24

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


#17063

From"Lew" <lew@1:261/38.remove-yy0-this>
Date2012-08-03 18:54 +0000
Message-ID<501C1564.56022.calajapr@time.synchro.net>
In reply to#16994
  To: bob smith
From: Lew <lewbloch@gmail.com>

 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

--- BBBS/Li6 v4.10 Dada-1
 * Origin: Prism bbs (1:261/38)
--- Synchronet 3.16a-Win32 NewsLink 1.98
Time Warp of the Future BBS - telnet://time.synchro.net:24

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


#17090

From"Roedy Green" <roedy.green@1:261/38.remove-yy0-this>
Date2012-08-03 18:54 +0000
Message-ID<501C1569.56049.calajapr@time.synchro.net>
In reply to#16994
  To: bob smith
From: Roedy Green <see_website@mindprod.com.invalid>

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

--- BBBS/Li6 v4.10 Dada-1
 * Origin: Prism bbs (1:261/38)
--- Synchronet 3.16a-Win32 NewsLink 1.98
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [standalone]


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


csiph-web