-3

Possible Duplicate:
Sorting an ArrayList of Contacts based on name?
Sort List<T> of objects by a particular rule?

Is there ant way to sort a generic ArrayList? I have a PlayerBean that includes fist name and stats I want to sort it by first name any idea? With a String array it was simple Collections.sort(list)...

 public class PlayerBean {
    private String name;
    private int stat1;
    private Double stat2;
    private Double stat3;
    private Double stat4;

        ....
 }
Community
  • 1
  • 1
user1163234
  • 2,407
  • 6
  • 35
  • 63
  • Also: [Sorting an ArrayList](http://stackoverflow.com/questions/4241829/sorting-an-arraylist) – Brian Roach Mar 14 '12 at 19:24
  • the reason people are downvoting is "research effort" which is the first reason given if you hover your mouse over the downvote button. This has already been asked and answered numerous times here on SO. – Brian Roach Mar 14 '12 at 19:27
  • have a look at http://docs.oracle.com/javase/6/docs/api/java/util/Collections.html#sort(java.util.List, java.util.Comparator) – andbi Mar 14 '12 at 19:29

2 Answers2

1

Use the same method with a specialized comparator:

 new Comparator<PlayerBean>() {

       @Override
       public int compare(PlayerBean o1, PlayerBean o2) {
           return o1.getName().compareTo(o2.getName());
       }                
 }
Karussell
  • 17,085
  • 16
  • 97
  • 197
0

Have a look at Collections.sort that takes a Comparator

Kristian
  • 6,443
  • 6
  • 27
  • 29