Giter Site home page Giter Site logo

tutorial-code-java-comparable-comparator's Introduction

Code Example for How to Use Comparator and Comparable


  • Created by Akash Tyagi on 5thFeb2021
  • Detail: How to use Comparable and Comparator interface to sort the arrays with simple or custom objects.

Comparable Example:

  • Employee Class with compareTo method implemented
package comparable.example;

public class Employee implements Comparable<Employee>{

    String name;
    int age;

    public Employee(String name, int age){
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString(){
        return this.name + " " + this.age;
    }

    @Override
    public int compareTo(Employee o) {

        if (this.age<o.age){
            return -1;
        }else if (this.age==o.age){
            return 0;
        }else {
            return 1;
        }
    }
}
  • Comparator Class
package comparable.example;
import java.util.ArrayList;
import java.util.Collections;

public class ComparableExample {

    public static void main(String[] args){
        Employee emp1 = new Employee("Akash",35);
        Employee emp2 = new Employee("SunShun",25);
        Employee emp3 = new Employee("Keerthana",26);
        Employee emp4 = new Employee("Timothy",29);
        Employee emp5 = new Employee("Maverick",38);

        //Create a Array list
        ArrayList<Employee> arrayList = new ArrayList();
        arrayList.add(emp1);
        arrayList.add(emp2);
        arrayList.add(emp3);
        arrayList.add(emp4);
        arrayList.add(emp5);

        //Sort the List
        Collections.sort(arrayList);
        System.out.println(arrayList);

    }
}

Comparator Example for Sorting:

  • Employee Class:
package comparator.example;

public class Employee1 implements Comparable<Employee1>{

    String name;
    int age;
    int salary;

    public Employee1(String name, int age, int salary){
        this.name = name;
        this.age = age;
        this.salary = salary;
    }

    @Override
    public String toString(){
        return this.name + " " + this.age + " " + this.salary;
    }

    @Override
    public int compareTo(Employee1 o) {

        if (this.age<o.age){
            return -1;
        }else if (this.age==o.age){
            return 0;
        }else{
            return 1;
        }
    }
}
  • Employee Age Comparator
package comparator.example;

import java.util.Comparator;

public class EmployeeAgeComparator implements Comparator<Employee1> {

    @Override
    public int compare(Employee1 o1, Employee1 o2) {
        if (o1.age>o2.age){
            return 1;
        }else if (o1.age<o2.age){
            return -1;
        }else{
            return 0;
        }
    }
}
  • Employee Salary Comparator
package comparator.example;

import java.util.Comparator;

public class EmployeeSalaryComparator implements Comparator<Employee1> {

    @Override
    public int compare(Employee1 o1, Employee1 o2) {
        if (o1.salary>o2.salary){
            return 1;
        }else if (o1.salary<o2.salary){
            return -1;
        }else{
            return 0;
        }
    }
}
  • Comparator Example, sort by Age and Salary
package comparator.example;


import java.util.ArrayList;
import java.util.Collections;

public class ComparatorExample {

    public static void main(String[] args){
        Employee1 emp1 = new Employee1("Akash",35,1500);
        Employee1 emp2 = new Employee1("SunSHun",25,3500);
        Employee1 emp3 = new Employee1("Tim",29,3000);
        Employee1 emp4 = new Employee1("Keerthana",26,500);
        Employee1 emp5 = new Employee1("Naverick",38,4000);

        //Create a Array list
        ArrayList<Employee1> arrayList = new ArrayList<Employee1>();
        arrayList.add(emp1);
        arrayList.add(emp2);
        arrayList.add(emp3);
        arrayList.add(emp4);
        arrayList.add(emp5);

        //Sort the array, by default based on age as mentioned in the Employee Class using Comparable
        Collections.sort(arrayList);
        System.out.println("Sorting based on Age: " + arrayList.toString());

        //Sort based on Salary Comparator
        Collections.sort(arrayList,new EmployeeSalaryComparator());
        System.out.println("Sorting based on Salary as: " + arrayList.toString());

    }
}

tutorial-code-java-comparable-comparator's People

Contributors

akashdktyagi avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.