Privacy Policy Terms Of Use. Copyright © 2006-2010 Java Tutorials and Examples.
Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective owners.
- package examples;
- /*
- Java Comparator example.
- This Java Comparator example describes how java.util.Comparator
- interface is implemented to compare Java custom class objects.
- This Java Comparator is passed to Collection's sorting method
- (for example Collections.sort method) to perform sorting of Java custom
- class objects.
- */
- import java.util.*;
- /*
- java.util.Comparator interface declares two methods,
- 1) public int compare(Object object1, Object object2) and
- 2) boolean equals(Object object)
- */
- /*
- We will compare objects of the Employee class using custom comparators
- on the basis of employee age and name.
- */
- class Employee{
- private int age;
- private String name;
- public void setAge(int age){
- this.age=age;
- }
- public int getAge(){
- return this.age;
- }
- public void setName(String name){
- this.name=name;
- }
- public String getName(){
- return this.name;
- }
- }
- /*
- User defined Java comparator.
- To create custom java comparator, implement Comparator interface and
- define compare method.
- The below given comparator compares employees on the basis of their age.
- */
- class AgeComparator implements Comparator{
- public int compare(Object emp1, Object emp2){
- /*
- * parameter are of type Object, so we have to downcast it
- * to Employee objects
- */
- int emp1Age = ((Employee)emp1).getAge();
- int emp2Age = ((Employee)emp2).getAge();
- if(emp1Age > emp2Age)
- return 1;
- else if(emp1Age < emp2Age)
- return -1;
- else
- return 0;
- }
- }
- /*
- The below given comparator compares employees on the basis of their name.
- */
- class NameComparator implements Comparator{
- public int compare(Object emp1, Object emp2){
- //parameter are of type Object, so we have to downcast it to Employee objects
- String emp1Name = ((Employee)emp1).getName();
- String emp2Name = ((Employee)emp2).getName();
- //uses compareTo method of String class to compare names of the employee
- return emp1Name.compareTo(emp2Name);
- }
- }
- /*
- This Java comparator example compares employees on the basis of
- their age and name and sort them in that order.
- */
- public class JavaComparatorExample{
- public static void main(String args[]){
- //Employee array which will hold employees
- Employee employee[] = new Employee[2];
- //set different attributes of the individual employee.
- employee[0] = new Employee();
- employee[0].setAge(40);
- employee[0].setName("Joe");
- employee[1] = new Employee();
- employee[1].setAge(20);
- employee[1].setName("Mark");
- System.out.println("Order of employee before sorting is");
- //print array as is.
- for(int i=0; i < employee.length; i++){
- System.out.println( "Employee " + (i+1) + " name :: " + employee[i].getName() + ", Age :: " + employee[i].getAge());
- }
- /*
- Sort method of the Arrays class sorts the given array.
- Signature of the sort method is,
- static void sort(Object[] object, Comparator comparator)
- IMPORTANT: All methods defined by Arrays class are static. Arrays class
- serves as a utility class.
- */
- //Sorting array on the basis of employee age by passing AgeComparator
- Arrays.sort(employee, new AgeComparator());
- System.out.println("\n\nOrder of employee after sorting by employee age is");
- for(int i=0; i < employee.length; i++){
- System.out.println( "Employee " + (i+1) + " name :: " + employee[i].getName() + ", Age :: " + employee[i].getAge());
- }
- //Sorting array on the basis of employee Name by passing NameComparator
- Arrays.sort(employee, new NameComparator());
- System.out.println("\n\nOrder of employee after sorting by employee name is");
- for(int i=0; i < employee.length; i++){
- System.out.println( "Employee " + (i+1) + " name :: " + employee[i].getName() + ", Age :: " + employee[i].getAge());
- }
- }
- }
- /*
- OUTPUT of the above given Java Comparable Example would be :
- Order of employee before sorting is
- Employee 1 name :: Joe, Age :: 40
- Employee 2 name :: Mark, Age :: 20
- Order of employee after sorting by employee age is
- Employee 1 name :: Mark, Age :: 20
- Employee 2 name :: Joe, Age :: 40
- Order of employee after sorting by employee name is
- Employee 1 name :: Joe, Age :: 40
- Employee 2 name :: Mark, Age :: 20
- */

Cool example.
Thanks for your explanation
very good explanation
Good example.......Thanks
very Good One.
nice demonstration .. like it
thanks a lot... saved a lot
Good Example..Thanks a lot
This is a very good Example
very nice example
Good Example
thanks cool nice one
To the point and broad
very good example .u are the
nice and simple to understand
thanks a lot ...........it
Great Example Thanks a lot
i want to know why we dont
Excelent example, simple,
Should implement Comparator
Should implement
+1. This article shows up as
nice example
Really nice. Thank you so
Good and stright example.
Its vey nice
Easy to understand :)
very very good example .
nice way to explain
A clean and clear Example.
Hi I have list of Beans with
nice one...
nice one...
nice example and easy to
nice
easy coding
easy coding
nice one ...thanks :)
nice
Really, its a damm good
Very excellent to understand
Really simple yet pawerful
Hi All, Has anyone executed
very good example
Thanks for Good Example
good example with explanation
Thanks for simple and
Nice
Thanks for a simple and
Good example.
Post new comment