This tutorial shows the use java.lang.Comparable and java.util.Comparator to sort arraylist and collection in java. The arraylist or collection may contain primitive data, string or user defined object. Here is is a good subject for any java programmer. Because if you know and understand how to sort arraylist and collection in Java(Comparable and Comparator) you will optimize your code
1. Sort an Array
1 2 3 4 5 6 7 8 9 |
String[] numbers = new String[] {"200","500", "450", "888"}; Arrays.sort(numbers); int i=0; System.out.println("**********Array of numbers is sorted**********"); for(String number: numbers){ System.out.println("number " + ++i + " : " + number); } |
Here is output
1 2 3 4 5 |
**********Array of numbers is sorted********** number 1 : 200 number 2 : 450 number 3 : 500 number 4 : 888 |
2. Sort an ArrayList
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
List<String> numbers = new ArrayList<String>(); numbers.add("200"); numbers.add("500"); numbers.add("450"); numbers.add("888"); Collections.sort(numbers); int i=0; System.out.println("**********Array of numbers is sorted**********"); for(String number: numbers){ System.out.println("number " + ++i + " : " + number); } |
Here is output
1 2 3 4 5 |
**********Array of numbers is sorted********** number 1 : 200 number 2 : 450 number 3 : 500 number 4 : 888 |
3. Sort Array of objects using Comparable
You should use Person class for sorting java object. The Person class implements the Comparable interface, and override the compareTo() method to compare its age property.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
package com.javabycode.sort; public class Person implements Comparable<Person>{ private String name; private Integer age; Person(String name, Integer age){ this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } @Override public int compareTo(Person p) { int compareAge = p.getAge(); //ascending order return this.age - compareAge; //descending order //return compareAge - this.age; } } |
You should use Arrays.sort() again to sort it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
package com.javabycode.sort; import java.util.Arrays; public class SortPersonSample { public static void main(String args[]){ Person[] persons = new Person[4]; Person person1 = new Person("Ronaldo",29); Person person2 = new Person("Messi",27); Person person3 = new Person("Pele",56); Person person4 = new Person("Maradona",48); persons[0]=person1; persons[1]=person2; persons[2]=person3; persons[3]=person4; Arrays.sort(persons); int i=0; System.out.println("**********Array of persons is sorted by age**********"); for(Person person: persons){ System.out.println("persons " + ++i + " : " + person.getName() + ", Age : " + person.getAge()); } } } |
Here is output
1 2 3 4 5 |
**********Array of persons is sorted by age********** persons 1 : Messi, Age : 27 persons 2 : Ronaldo, Age : 29 persons 3 : Maradona, Age : 48 persons 4 : Pele, Age : 56 |
4. Sort Array of Object using Comparator
You could only sort a single property with the Comparable interface. To sort with multiple properties, you should use Comparator. Here you adapt the method PersonNameComparator implemented Comparator into the class Person.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
public static Comparator<Person> PersonNameComparator = new Comparator<Person>() { public int compare(Person person1, Person person2) { String personName1 = person1.getName().toUpperCase(); String personName2 = person2.getName().toUpperCase(); //ascending order return personName1.compareTo(personName2); //descending order //return personName2.compareTo(personName1); } }; |
Sort Person array with its “name” property in ascending order
1 |
Arrays.sort(persons, Person.PersonNameComparator); |
Here is output
1 2 3 4 5 |
**********Array of persons is sorted by age********** persons 1 : Maradona, Age : 48 persons 2 : Messi, Age : 27 persons 3 : Pele, Age : 56 persons 4 : Ronaldo, Age : 29 |
If you store person objects in a collection you could sort by using this code sample below
1 2 3 4 5 6 7 8 9 10 11 12 13 |
List<Person> persons = new ArrayList<Person>(); Person person1 = new Person("Ronaldo",29); Person person2 = new Person("Messi",27); Person person3 = new Person("Pele",56); Person person4 = new Person("Maradona",48); persons.add(person1); persons.add(person2); persons.add(person3); persons.add(person4); Collections.sort(persons); Collections.sort(persons, Person.PersonNameComparator); |
I hope that this tutorial sort arraylist and collection in java is useful for you.
Happy learning!