Sorting Objects in an ArrayList

Hello all,I have an ArrayList with objects that I want to sort. I would like to sort them by a string field. I'm just not sure how to do the comparisons to lexicographically sort them.Thanks for any help!
[227 byte] By [kjkluever] at [2007-9-30 20:55:44]
# 1

> Hello all,

> I have an ArrayList with objects that I want to sort.

> I would like to sort them by a string field. I'm just

> not sure how to do the comparisons to

> lexicographically sort them.

> Thanks for any help!

Let's say the objects you're storing are of type Foo, and they have a member variable called bar that is the String you want to sort on. Declare Foo to implement Comparable, and then write your compareTo() method to return the results of this.bar.compareTo(obj.bar). This works because String itself implements comparable. Of course, if you want a different ordering than String's "natural" ordering, you'll have to do more work.

Then you just call Collections.sort(yourArrayList)

jverd at 2007-7-7 2:28:21 > top of Java-index,Java Essentials,Java Programming...
# 2

java.util.Collections.sort(

list,

new java.util.Comparator() {

public int compare(Object o1, Object o2) {

// may need to handle null as well

return o1.getString().compareTo(o2.getString());

}

}

);

hwaite at 2007-7-7 2:28:21 > top of Java-index,Java Essentials,Java Programming...
# 3
Look into Collections.sort(). If your objects are Comparable, there will be no problem. Otherwise, you may have to make a Comparator to tell how to sort them, but it can be as easy as myObj1.stringField.compareTo(myObj2.stringField);
jboeing at 2007-7-7 2:28:21 > top of Java-index,Java Essentials,Java Programming...
# 4
[url= http://java.sun.com/docs/books/tutorial/collections/interfaces/order.html]The Java™ Tutorial - Object Ordering[/url][url= http://www.onjava.com/lpt/a/3286]Making Java Objects Comparable[/url]
yawmark at 2007-7-7 2:28:21 > top of Java-index,Java Essentials,Java Programming...
# 5

It works for the first three items I enter and then it starts messing up.

Here is my code for the compareTo method:

public int compareTo(Object in_lp){

LP temp_lp = (LP) in_lp;

return (Band.compareTo(temp_lp.getBand()));

}

I don't know what could be going wrong.

kjkluever at 2007-7-7 2:28:21 > top of Java-index,Java Essentials,Java Programming...
# 6

> It works for the first three items I enter and then it

> starts messing up.

> Here is my code for the compareTo method:

> > public int compareTo(Object in_lp){

> LP temp_lp = (LP) in_lp;

> return (Band.compareTo(temp_lp.getBand()));

> }

>

> I don't know what could be going wrong.

There are a couple of problems with this post:

1) None of us knows what "messing up" means. Describe what you expect to happen vs. what actually happens. For bonus points, summplement with more code including print statements that show what's happening and what values are being set & used and couch your expected/observed comments in terms of those print statemetns.

2) WTF is Band? None of us knows anything about that class.

jverd at 2007-7-7 2:28:21 > top of Java-index,Java Essentials,Java Programming...
# 7
> 2) WTF is Band? None of us knows anything about that> class.I am guessing that it is (shock, horror) an instance variable in the class containing that code. Amazing how Sun's coding conventions direct your thoughts, isn't it?
DrClap at 2007-7-7 2:28:21 > top of Java-index,Java Essentials,Java Programming...
# 8

> > 2) WTF is Band? None of us knows anything about that

> > class.

>

> I am guessing that it is (shock, horror) an instance

> variable in the class containing that code. Amazing

> how Sun's coding conventions direct your thoughts,

> isn't it?

Yep. Noticed that after I posted.

jverd at 2007-7-7 2:28:21 > top of Java-index,Java Essentials,Java Programming...
# 9

I'm sorry about the previous post guys.

Here are the results of a sort I tried to do:

Led Zeppelin -- Physical Graffiti -- 12" -- Serial #40385 -- $5.0 -- NM

Led Zeppelin -- Zoso -- 12" -- Serial #324908 -- $5.0 -- NM

Led Zeppelin -- Three -- 12" -- Serial #35243 -- $4.0 -- NM

Led Zeppelin -- Houses Of The Holy -- 12" -- Serial #02483 -- $5.0 -- NM

Led Zeppelin -- One -- 12" -- Serial #143 -- $4.0 -- NM

kjkluever at 2007-7-7 2:28:21 > top of Java-index,Java Essentials,Java Programming...
# 10
That still doesn't help much.We don't know what you're printing or where. We don't know what you expect to see instead of that.
jverd at 2007-7-7 2:28:21 > top of Java-index,Java Essentials,Java Programming...
# 11
Is that the way it's supposed to sort those things, Physical Graffiti, One, Three, Houses of the Holy, etc?
kjkluever at 2007-7-7 2:28:21 > top of Java-index,Java Essentials,Java Programming...
# 12

> Is that the way it's supposed to sort those things,

> Physical Graffiti, One, Three, Houses of the Holy,

> etc?

It's supposed to sort them however you decide that it should. How to you want to see it sorted? Relase year, then band, then album? Band then album then price?

Your compareTo method is doing exactly what you told it to do--comparing based on the band name only. If you want to compare on other fields, then your compareTo should check the first field, if its unequal, return the results right there, and if equal, then move on to the next field. Repeat for each field you want to use for the comparison.

Think about how you would compare two albums. Write the instructions down as if you're explaining to somebody else how to do it. Then translate those instructions to Java code.

jverd at 2007-7-7 2:28:21 > top of Java-index,Java Essentials,Java Programming...
# 13
If "Band" contains "Led Zeppelin", and if your code sorts on Band, then the list is sorted correctly.
DrClap at 2007-7-7 2:28:21 > top of Java-index,Java Essentials,Java Programming...
# 14
(*&$@*(&$^#*&#^I'm sorry I acted like such a *****. I wanted to say Album.compareTo(lp.getAlbum());I already have them sorted into arraylists by band. Thanks for all your help guys.
kjkluever at 2007-7-7 2:28:21 > top of Java-index,Java Essentials,Java Programming...
# 15
Explaining how to sort -- 5 minutesClarifying -- 2 minutesWitnessing somebody else's "D'OH!" moment -- Priceless.
jverda at 2007-7-20 0:13:36 > top of Java-index,Java Essentials,Java Programming...