• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Sorting an Arrylist<MyType>

 
Martin Vietor
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Greg Charles
Sheriff
Posts: 2989
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20611
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Martin Vietor
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20611
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic