1

Possible Duplicate:
Sorting an ArrayList of Contacts based on name?
Sorting of ArrayList

I have an ArrayList of type Person and I want to sort it based on the person's ages: person.getAge().

How can I do that, so that the list of people is ordered by their age?

Community
  • 1
  • 1
124697
  • 22,097
  • 68
  • 188
  • 315

4 Answers4

2
ArrayList<Person> people = . . .;
Collections.sort(people, new Comparator<Person>() {
        @Override
        public int compare(Person o1, Person o2) {
            final int age1 = o1.getAge();
            final int age2 = o2.getAge();
            return age1 < age2 ? -1 : age1 > age2 ? 1 : 0;
        }
    }
);

In Java 7, you can use return Integer.compare(age1, age2); instead.

Alternatively, you can have Person implement Comparable<Person>. However, then you could only sort on the one attribute.

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
1

Expanding on the comment, something along these lines:

Collections.sort(personList, new Comparator<Person>() {
    @Override
    public int compare(Person p1, Person p2) {
        return new Integer(p1.getAge()).compareTo(p2.getAge());
    }
});

Relevant documentation

arshajii
  • 127,459
  • 24
  • 238
  • 287
0

Define your comparator, which compares using the age attribute Only. Use the same in collection sort method [sort(java.util.List, java.util.Comparator)](http://docs.oracle.com/javase/7/docs/api/java/util/Collections.html#sort(java.util.List, java.util.Comparator)) which accepts the comparator.

        Collections.sort(peopleList, new Comparator<Person>(){
             @Override
             public int compare(Person p1, Person p2) {
                return Integer.valueOf(p1.getAge()).compareTo(p2.getAge());
             }
         });

Or better do below for optimization as Integer.compareTo internally uses Integer.compare method.

       Collections.sort(peopleList, new Comparator<Person>(){
             @Override
             public int compare(Person p1, Person p2) {
                return Integer.compare(p1.getAge(), p2.getAge());
             }
         });
Yogendra Singh
  • 33,927
  • 6
  • 63
  • 73
  • This generates a lot of garbage because it creates two `Integer` objects for each comparison. Much better to stick to primitive `int` comparisons. – Ted Hopp Dec 02 '12 at 19:29
  • @TedHopp Updated the answer with optimized version as well. – Yogendra Singh Dec 02 '12 at 19:34
  • @user521180 You can better use `Integer.compare` method which does the comparison of tow int values for you. I would advice not to implement a custom comparison. – Yogendra Singh Dec 02 '12 at 19:42
0
//We can do using TreeMap
//i.e
TreeMap treemap=new TreeMap<int,Person>
//trace over person
for(Person p:YourPersonArrayList)
{
treemap.put(p.getAge(),p);
}
//Finally you get TreeMap which is 
//sorted on Persons age...
Nagappa L M
  • 1,452
  • 4
  • 20
  • 33
  • Code may not work because i am not sure about int key's in TreeMap,you can use TreeMap Collection to solve your problem – Nagappa L M Dec 02 '12 at 19:35