Sort arraylist and collection in java (Comparable and Comparator)

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=newString[]{"200","500","450","888"};

Arrays.sort(numbers);

inti=0;

System.out.println("**********Array of numbers is sorted**********");

for(Stringnumber:numbers){

System.out.println("number "+++i+" : "+number);

}

Here is output

1

2

3

4

5

**********Arrayof numbers issorted**********

number1:200

number2:450

number3:500

number4:888

2. Sort an ArrayList

1

2

3

4

5

6

7

8

9

10

11

12

13

14

List<String>numbers=newArrayList<String>();

numbers.add("200");

numbers.add("500");

numbers.add("450");

numbers.add("888");

Collections.sort(numbers);

inti=0;

System.out.println("**********Array of numbers is sorted**********");

for(Stringnumber:numbers){

System.out.println("number "+++i+" : "+number);

}

Here is output

1

2

3

4

5

**********Arrayof numbers issorted**********

number1:200

number2:450

number3:500

number4: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

packagecom.javabycode.sort;

publicclassPersonimplementsComparable<Person>{

privateStringname;

privateIntegerage;

Person(Stringname,Integerage){

this.name=name;

this.age=age;

}

publicStringgetName(){

returnname;

}

publicvoidsetName(Stringname){

this.name=name;

}

publicIntegergetAge(){

returnage;

}

publicvoidsetAge(Integerage){

this.age=age;

}

@Override

publicintcompareTo(Personp){

intcompareAge=p.getAge();

//ascending order

returnthis.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

packagecom.javabycode.sort;

import java.util.Arrays;

publicclassSortPersonSample{

publicstaticvoidmain(Stringargs[]){

Person[]persons=newPerson[4];

Person person1=newPerson("Ronaldo",29);

Person person2=newPerson("Messi",27);

Person person3=newPerson("Pele",56);

Person person4=newPerson("Maradona",48);

persons[0]=person1;

persons[1]=person2;

persons[2]=person3;

persons[3]=person4;

Arrays.sort(persons);

inti=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

**********Arrayof persons issorted by age**********

persons1:Messi,Age:27

persons2:Ronaldo,Age:29

persons3:Maradona,Age:48

persons4: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.