verbose sort

B

bob smith

From: bob smith <[email protected]>

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
 
M

markspace

To: bob smith
From: 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 );

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

Eric Sosman

To: markspace
From: Eric Sosman <[email protected]>

Collections.sort( my_list, String.CASE_INSENSITIVE_ORDER );

Throws NullPointerException if the list has any nulls.

--
Eric Sosman
(e-mail address removed)

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

markspace

To: Eric Sosman
From: 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.

--- 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
 
V

Volker Borchert

Eric said:
To: bob smith
From: Eric Sosman <[email protected]>


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

I'd do it as a fastpath and GoF Decorator

public final class NullFirstComparator<T> implements Comparator<T> {
@NonNull
private final Comparator<T> delegate;

public NullFirstComparator(@NonNull final Comparator<T> delegate) {
this.delegate = delegate;
}

public int compare(final T t1, final T t2) {
if (t1 == t2) {
return 0;
} else if (t1 == null) {
return -1;
} else if (t2 == null) {
return 1;
} else {
return delegate.compare(t1, t2);
}
}
}

Collections.sort(my_list, new NullFirstComparator<String>(String.CASE_INSENSITIVE_ORDER));
 
D

Daniel Pitts

To: markspace
From: Daniel Pitts <[email protected]>

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
 
L

Lew

To: bob smith
From: Lew <[email protected]>

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.

--
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
 
B

bob smith

To: markspace
From: bob smith <[email protected]>

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
 
R

Roedy Green

To: bob smith
From: Roedy Green <[email protected]>

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)

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

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

Similar Threads

verbose sort 8
multiple inheritance 14
multiple inheritance 5
multiple inheritance 6
Bluetooth programming 0
Bluetooth programming 0
multiple inheritance 5
hashCode 50

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top