Visit Software Engineering Space for more articles

Sorting list of objects with java.util.Comparator

Sort a list of objects in different ways.

Code Explanation

The java.util.Comparator interface imposes an ordering on a collection of objects. Comparator objects can be passed to sort methods (e.g. Collections#sort()) to control over the sort order. Therefore, a single list of objects can be sorted differently using different Comparators.

Let us have Dog object and every dog has a name, age and sex. Then in DogSorter class we would create a list of dogs and then sort this list using different Comparators.

  9 public class Dog {
 10
 11     /**
 12      * An enum of sex.
 13      */
 14     public enum Sex {
 15
 16         /**
 17          * Male.
 18          */
 19         MALE,
 20         /**
 21          * Female.
 22          */
 23         FEMALE;
 24     }
 25     /**
 26      * Name.
 27      */
 28     private String name;
 29     /**
 30      * Age.
 31      */
 32     private int age;
 33     /**
 34      * Sex.
 35      */
 36     private Sex sex;
 37
 38     /**
 39      * Constructs a new Dog instance.
 40      * @param name Name of the dog.
 41      * @param age Age of the dog.
 42      * @param sex Sex of the dog.
 43      */
 44     public Dog(String name, int age, Sex sex) {
 45         this.name = name;
 46         this.age = age;
 47         this.sex = sex;
 48     }
102 }
    
    

Now we would define a Comparator which compares two given dogs based on their name.

11 public class DogNameComparator implements Comparator<Dog> {
12
13     @Override
14     public int compare(Dog o1, Dog o2) {
15         return o1.getName().compareTo(o2.getName());
16     }
17 }
18
    

Let us define another Comparator which compares two given dogs based on their age.

11 public class DogAgeComaparator implements Comparator<Dog> {
12
13     @Override
14     public int compare(Dog o1, Dog o2) {
15         return o1.getAge() - o2.getAge();
16     }
17 }
18
    

The main() method of DogSorter class creates multiple dog objects and adds them to a list. And then sort() method is called by passing the dog list and an appropriate Comparator.

21     public static void main(String[] args) {
22         Dog dog1 = new Dog("Magic", 5, Sex.MALE);
23         Dog dog2 = new Dog("Ajax", 8, Sex.MALE);
24         Dog dog3 = new Dog("Alpha", 6, Sex.FEMALE);
25         Dog dog4 = new Dog("Gigolo", 1, Sex.MALE);
26         List<Dog> dogs = new ArrayList<Dog>();
27         dogs.add(dog1);
28         dogs.add(dog2);
29         dogs.add(dog3);
30         dogs.add(dog4);
31         DogSorter sortTest = new DogSorter();
32         sortTest.printDogs("############# List Of Dogs #############", dogs);
33         sortTest.printDogs(
34                 "############# Sorted By Name : Ascending #############", sortTest.
35                 sort(
36                 dogs, new DogNameComparator()));
37         sortTest.printDogs("############# Sorted By Age : Ascending ###########", sortTest.
38                 sort(
39                 dogs, new DogAgeComaparator()));
40     }
41 
    

The sort() method which calls java.util.Collections#sort() to sort the list.

49     private List<Dog> sort(List<Dog> dogs,
50             Comparator<Dog> dogComp) {
51         Collections.sort(dogs, dogComp);
52         return dogs;
53     }
54 
    

Input and Output

Following Dogs were given as input:

    "Magic", 5, MALE
    "Ajax", 8, MALE
    "Alpha", 6, FEMALE
    "Gigolo", 1, MALE
    

The output of the program is:

    ############# List Of Dogs #############
    name: Magic  age: 5  sex: MALE
    name: Ajax  age: 8  sex: MALE
    name: Alpha  age: 6  sex: FEMALE
    name: Gigolo  age: 1  sex: MALE

    ############# Sorted By Name : Ascending #############
    name: Ajax  age: 8  sex: MALE
    name: Alpha  age: 6  sex: FEMALE
    name: Gigolo  age: 1  sex: MALE
    name: Magic  age: 5  sex: MALE

    ############# Sorted By Age : Ascending ###########
    name: Gigolo  age: 1  sex: MALE
    name: Magic  age: 5  sex: MALE
    name: Alpha  age: 6  sex: FEMALE
    name: Ajax  age: 8  sex: MALE
    

Download

You may download the complete source code from here: