Sort Student Data using Comparable and Comparator

This is the demonstration on how to use java.util.Comparable and java.util.Comparator to sort data. In this example, I would using some example of student data, to sort it using both ways.

Comparable is an interface under java.util, the class of the student data must implements it, in order using Collections.sort() to sort it. The programmer need to implements the compareTo() method in order to achieve it.

Comparator is another interface under java.util, it provide more flexibility in sorting the data in many ways. With using Comparator,  the programmer is required to make another class which implements Comparator but no implements on class of student data. The benefits are we need not to modify the class of data being sorted, and also we can create as more as possible the Comparator class, provide many sorting method. Lastly, the programmer need to implements the compare() method in order to achieve it.

Here I using the Comparable to sort the Student Data. Below is the code and the output.

import java.util.*;

// Demonstrate using Comparable to sort the student data based on ID, Name, Age

class Student implements Comparable<Student> {
    private int ID;
    private String name;
    private int age;

    public Student(int i, String s, int a) {
        ID = i;
        name = s;
        age = a;
    }

    public int getID() {return ID;}
    public String getName() {return name;}
    public int getAge() {return age;}

    public int compareTo(Student stu) {
        // sort based on ID
        return this.ID – stu.ID;
    }

    public String toString() {
        return ID + "\t" + name + "\t" + age;
    }
}

public class StudentData {
    public static void main(String args[]) {
        List<Student> list = new ArrayList<Student>();
        list.add(new Student(3,"Mohan",50));
        list.add(new Student(2,"fyhao",20));
        list.add(new Student(5,"Ahmin",35));
        list.add(new Student(6,"Abang",32));
        list.add(new Student(1,"Meili",13));

        System.out.println("Before Sort:\nID\tName\tAge");
        for(Student stu : list) {
            System.out.println(stu);
        }

        Collections.sort(list);

        System.out.println("After Sort:\nID\tName\tAge");
        for(Student stu : list) {
            System.out.println(stu);
        }
    }
}

 

image

Where below is the implementation of the code using Comparator to sort it. I demonstrated it using three different sorting method, you all can check it out from below the code and the output.

import java.util.*;

// Demonstrate using Comparator to sort the student data based on ID, Name, Age

class Student {
    private int ID;
    private String name;
    private int age;

    public Student(int i, String s, int a) {
        ID = i;
        name = s;
        age = a;
    }

    public int getID() {return ID;}
    public String getName() {return name;}
    public int getAge() {return age;}

    public String toString() {
        return ID + "\t" + name + "\t" + age;
    }
}

class reverseAge implements Comparator<Student> {
    public int compare(Student s1, Student s2) {
        return s2.getAge() – s1.getAge();
    }
}

class reverseAlphabetical implements Comparator<Student> {
    public int compare(Student s1, Student s2) {
        return s2.getName().compareTo(s1.getName());
    }
}

class naturalID implements Comparator<Student> {
    public int compare(Student s1, Student s2) {
        return s1.getID() – s2.getID();
    }
}

public class StudentData {
    public static void main(String args[]) {
        List<Student> list = new ArrayList<Student>();
        list.add(new Student(3,"Mohan",50));
        list.add(new Student(2,"fyhao",20));
        list.add(new Student(5,"Ahmin",35));
        list.add(new Student(6,"Abang",32));
        list.add(new Student(1,"Meili",13));

        System.out.println("Before Sort:\nID\tName\tAge");
        for(Student stu : list) {
            System.out.println(stu);
        }

        Collections.sort(list, new reverseAge());

        System.out.println("After Sort (Reverse Age):\nID\tName\tAge");
        for(Student stu : list) {
            System.out.println(stu);
        }

        Collections.sort(list, new reverseAlphabetical());

        System.out.println("After Sort (Reverse Name):\nID\tName\tAge");
        for(Student stu : list) {
            System.out.println(stu);
        }

        Collections.sort(list, new naturalID());

        System.out.println("After Sort (Natural ID):\nID\tName\tAge");
        for(Student stu : list) {
            System.out.println(stu);
        }
    }
}

 

image

All right, any more questions about this? I will waiting for your reply.

Author: fyhao

Jebsen & Jessen Comms Singapore INTI University College Bsc (Hon) of Computer Science, Coventry University

1 thought on “Sort Student Data using Comparable and Comparator”

Leave a Reply