Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to sort this list ?

 
Raj Ohadi
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a list of objects. each object is of type

class {
String name;
int id;
String address;
}

how do I sort the list by name then by id ? i.e. these two object have same names then sort by their id. This is like SQL's "order by name, id"

 
Maneesh Godbole
Saloon Keeper
Posts: 11070
13
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By list, you mean the java.util.List interface ?
You can use the java.util.Collections.sort method, and either make your class implement the Comparable interface, or make a Comparator class and pass ot to the sort method. You'll find a detailed explanation here.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic