It's not a secret anymore!*
The moose likes Beginning Java and the fly likes Sorting an Arrylist<MyType> Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Sorting an Arrylist<MyType>" Watch "Sorting an Arrylist<MyType>" New topic
Author

Sorting an Arrylist<MyType>

Martin Vietor
Ranch Hand

Joined: Oct 31, 2008
Posts: 40
Me again, hi y'all,

I now have the problem that I would like to sort a list. It is an Arraylist<PatientInfo> with the class PatientInfo looking like this:



I would like to sort the list by name. Is that possible? Collection.sort doesn"t work, because PateitnInfo is not per se sortable. I could write a method that sorts by name, I just want to know if there is an easier way, thanks.

M

The problem people have in trying to create something foolproof is that they generally underestimate the inginuity of fools.
Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2835
    
  11

Yes, you can, you just need to tell Java how MyType should be ordered. That's done by implementing the Comparable interface.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Greg Charles wrote:Yes, you can, you just need to tell Java how MyType should be ordered. That's done by implementing the Comparable interface.

Or by making a custom Comparator<PatientInfo>.
Martin Vietor
Ranch Hand

Joined: Oct 31, 2008
Posts: 40
Riiiight. That. Um. How?

public class PatientInfo() implements Comparable {
...
}

Ok. And a comparTo method. But I just don't get at all how to. Pointers will be fine, my (brief() internet search didn't really help.
M
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

The compareTo API says :
Compares this object with the specified object for order. Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.

You want to sort by the name of the patient. The name is a String. String is Comparable, so you can use its compareTo method to compare the name of two patients. Try to figure out how.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

Martin Vietor wrote:public class PatientInfo() implements Comparable

That should be You want its instances to be comparable to other instances, and that's what the generic part does. It then requires you to have the following method:
Without the generics the parameter type would be Object and you would need to cast it to PatientInfo.

As for the implementation, Christophe has already told you how to do it. Simply delegate the comparison to name comparison. Just one thing to think about: do you want to search case insensitively or not? If so, String has a second comparison method for this.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Martin Vietor
Ranch Hand

Joined: Oct 31, 2008
Posts: 40
Well thanks, here we go. I'll try to figure it out. I'm sort of a self taught "programmer", so some terms are very abstract to me. But it's always better to read up on that oneself than getting the code done by someone else, I think.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

 
wood burning stoves
 
subject: Sorting an Arrylist<MyType>
 
Similar Threads
how to sort this list ?
Simple rejectIfEmptyOrWhiteSpace validation
arrange in ascending order
Sorting MAP
Vectors and Loops