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?

asked Dec 2, 2012 at 19:21
2
  • 5
    Look into Comparator interface and Collections.sort(...) method Commented Dec 2, 2012 at 19:22
  • look this question stackoverflow.com/questions/1814095/… Commented Dec 2, 2012 at 19:24

4 Answers 4

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.

answered Dec 2, 2012 at 19:24

4 Comments

Did you mean the static Integer.compare(int, int)?
@PaulBellora - Yes, thanks. I'll fix the typo.
The last approach still works in Java 8. Most of the number classes have a compare method, I assume Boolean has the same as well (not checked)
@LunarWatcher - Yes, Boolean implements Comparable<Boolean>. It treats true as greater than false.
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

answered Dec 2, 2012 at 19:24

Comments

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...
answered Dec 2, 2012 at 19:31

1 Comment

Code may not work because i am not sure about int key's in TreeMap<int,Person>,you can use TreeMap Collection to solve your problem
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());
 }
 });
answered Dec 2, 2012 at 19:27

3 Comments

This generates a lot of garbage because it creates two Integer objects for each comparison. Much better to stick to primitive int comparisons.
@TedHopp Updated the answer with optimized version as well.
@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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.