Using Comparators to sort objects in Java

Comparator interface is used to order the objects of user-defined classes. A comparator object is capable of comparing two objects of two different classes. Following comparator will sort by ID

Example

package collections;

import java.util.List;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

class Student {
	int ID;
	String name;
	char grade;

	public Student(int iD, String name, char grade) {
		super();
		ID = iD;
		this.name = name;
		this.grade = grade;
	}

	@Override
	public String toString() {
		return "Student [ID=" + ID + ", name=" + name + ", grade=" + grade + "]";
	}
}


class IdComparator implements Comparator<Student> {

    @Override
    public int compare(Student o1, Student o2) {
    	int result = o1.ID - o2.ID; // Sorting by ID
    	return result;
    }
}

public class StudentSort {

	public static void main(String[] args) {

		List<Student> stu = new ArrayList<>();
		stu.add(new Student(10, "General Motors", 'A'));
		stu.add(new Student(100, "Ferrari", 'B'));
		stu.add(new Student(5, "Mustang", 'C'));
		stu.add(new Student(5, "Ford", 'F'));

		System.out.println("Before");
		printCollections(stu);
		Collections.sort(stu, new IdComparator());
		System.out.println("After");
		printCollections(stu);
	}

	private static void printCollections(List<Student> stu) {
		for (Student s : stu)
			System.out.println(s);
	}
}

Output

Before
Student [ID=10, name=General Motors, grade=A]
Student [ID=100, name=Ferrari, grade=B]
Student [ID=5, name=Mustang, grade=C]
Student [ID=5, name=Ford, grade=F]
After
Student [ID=5, name=Mustang, grade=C]
Student [ID=5, name=Ford, grade=F]
Student [ID=10, name=General Motors, grade=A]
Student [ID=100, name=Ferrari, grade=B]

You can define multiple comparators to sort object for different parameters

Example

package collections;

import java.util.List;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

class Student {
	int ID;
	String name;
	char grade;

	public Student(int iD, String name, char grade) {
		super();
		ID = iD;
		this.name = name;
		this.grade = grade;
	}

	@Override
	public String toString() {
		return "Student [ID=" + ID + ", name=" + name + ", grade=" + grade + "]";
	}
}

class IdComparator implements Comparator<Student> {

	@Override
	public int compare(Student o1, Student o2) {
		int result = o1.ID - o2.ID; // Sorting by ID
		return result;
	}
}

class NameComparator implements Comparator<Student> {

	@Override
	public int compare(Student o1, Student o2) {
		int result = o1.name.compareTo(o2.name); // Sorting by name
		return result;
	}
}

public class StudentSort {

	public static void main(String[] args) {

		List<Student> stu = new ArrayList<>();
		stu.add(new Student(10, "General Motors", 'A'));
		stu.add(new Student(100, "Ferrari", 'B'));
		stu.add(new Student(5, "Mustang", 'C'));
		stu.add(new Student(5, "Ford", 'F'));

		System.out.println("Original List");
		printCollections(stu);
		Collections.sort(stu, new IdComparator()); // comparing by ID
		System.out.println("After Comparing by ID");
		printCollections(stu);
		Collections.sort(stu, new NameComparator()); // comparing by Name
		System.out.println("After Comparing by Name");
		printCollections(stu);
	}

	private static void printCollections(List<Student> stu) {
		for (Student s : stu)
			System.out.println(s);
	}
}

Output

Original List
Student [ID=10, name=General Motors, grade=A]
Student [ID=100, name=Ferrari, grade=B]
Student [ID=5, name=Mustang, grade=C]
Student [ID=5, name=Ford, grade=F]
After Comparing by ID
Student [ID=5, name=Mustang, grade=C]
Student [ID=5, name=Ford, grade=F]
Student [ID=10, name=General Motors, grade=A]
Student [ID=100, name=Ferrari, grade=B]
After Comparing by Name
Student [ID=100, name=Ferrari, grade=B]
Student [ID=5, name=Ford, grade=F]
Student [ID=10, name=General Motors, grade=A]
Student [ID=5, name=Mustang, grade=C]

Leave a Reply

Your email address will not be published. Required fields are marked *