• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Sorting an Arrylist<MyType>

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 3063
12
Mac IntelliJ IDE Python VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
If you look closely at this tiny ad, you will see five bicycles and a naked woman:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic