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