Comparable vs Comparator in Java

Adil Abdullah
3 min readMar 28, 2023

Comparable

Create Person model class implements Comparable interface and compare with all of 3 age, name, salary fields like name for string, age for integer, and salary for double.

Person Model Class

public class Person implements Comparable<Person>{

String name;

int age;

double salary;

String city;

@Override

public String toString() {

return “Person [name=” + name + “, age=” + age + “, salary=” + salary + “, city=” + city + “]”;

}

public Person(String name,double salary, int age,String city) {

super();

this.name = name;

this.salary = salary;

this.age = age;

this.city = city;

}

public Person() {

super();

// TODO Auto-generated constructor stub

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public int getAge() {

return age;

}

public void setAge(int age) {

this.age = age;

}

public double getSalary() {

return salary;

}

public void setSalary(double salary) {

this.salary = salary;

}

public String getCity() {

return city;

}

public void setCity(String city) {

this.city = city;

}

// Method for sorting object on the basis of age int value

/* public int compareTo(Person p) {

if(this.age==p.age)

{

return 0;

}

else if(this.age>p.age)

{

return 1;

}

else

{

return -1;

}

} */

// Method for sorting object on the basis of salary float value

/* public int compareTo(Person p) {

if(this.salary==p.salary)

{

return 0;

}

else if(this.salary>p.salary)

{

return 1;

}

else

{

return -1;

}

} */

// Method for sorting object on the basis of name String value

public int compareTo(Person p) {

return this.name.compareTo(p.getName());

}

}

TestComparable Class

package com.java.comparable;

import java.util.*;

public class TestComparable {

List<Person> li=new ArrayList<Person>();

TestComparable()

{

li.add(new Person(“zain”, 21323.34, 25, “KHI”));

li.add(new Person(“haris”, 61387.00, 31, “LHR”));

li.add(new Person(“adil”, 341.21, 28, “KHI”));

li.add(new Person(“faizan”, 8265.94, 29, “ISB”));

li.add(new Person(“raza”, 8235.42, 32, “KHI”));

li.add(new Person(“jaffar”, 8745.21, 31, “QTA”));

li.add(new Person(“hunain”, 5645.23, 24, “PES”));

}

public void display()

{

Collections.sort(li);

for(Person per:li)

{

System.out.println(per);

}

}

public static void main(String[] args)

{

new TestComparable().display();

}

}

Comparator

Create Person model class implements Comparator interface and compare with all of 3 age, name, salary fields like name for string, age for integer, and salary for double.

TestComparator Class

package com.java.comparator;

import java.util.ArrayList;

import java.util.Collections;

import java.util.Comparator;

import java.util.List;

import com.java.comparable.Person;

import com.java.comparable.TestComparable;

public class TestComparator {

List<Person> li=new ArrayList<Person>();

TestComparator()

{

li.add(new Person(“zain”, 21323.34, 25, “KHI”));

li.add(new Person(“haris”, 61387.00, 31, “LHR”));

li.add(new Person(“adil”, 341.21, 28, “KHI”));

li.add(new Person(“faizan”, 8265.94, 29, “ISB”));

li.add(new Person(“raza”, 8235.42, 32, “KHI”));

li.add(new Person(“jaffar”, 8745.21, 31, “QTA”));

li.add(new Person(“hunain”, 5645.23, 24, “PES”));

}

public void display()

{

// Anonymous object for sorting object on the basis of age int value

/* Collections.sort(li,new Comparator<Person>(){

public int compare(Person p1, Person p2) {

return p1.getAge()-p2.getAge();

}

}); */

// Anonymous object for sorting object on the basis of salary float value

/* Collections.sort(li,new Comparator<Person>(){

public int compare(Person p1, Person p2) {

return (int) (p1.getSalary()-p2.getSalary());

}

}); */

// Anonymous object for sorting object on the basis of name string value

Collections.sort(li,new Comparator<Person>(){

public int compare(Person p1, Person p2) {

return p1.getName().compareTo(p2.getName());

}

});

for(Person per:li)

{

System.out.println(per);

}

}

public static void main(String[] args)

{

new TestComparator().display();

}

}

--

--

Adil Abdullah

Currently working as Full stack Java developer at Contour Software at their Easit AB division part of Jonas group. Working on Java and IBM stack.