You just have to define that Animal implements Comparable<Animal> i.e. public class Animal implements Comparable<Animal>. And then you have to implement the compareTo(Animal other) method that way you like it.
@Override
public int compareTo(Animal other) {
return Integer.compare(this.year_discovered, other.year_discovered);
}
Using this implementation of compareTo, animals with a higher year_discovered will get ordered higher. I hope you get the idea of Comparable and compareTo with this example.
Videos
You just have to define that Animal implements Comparable<Animal> i.e. public class Animal implements Comparable<Animal>. And then you have to implement the compareTo(Animal other) method that way you like it.
@Override
public int compareTo(Animal other) {
return Integer.compare(this.year_discovered, other.year_discovered);
}
Using this implementation of compareTo, animals with a higher year_discovered will get ordered higher. I hope you get the idea of Comparable and compareTo with this example.
You need to:
- Add
implements Comparable<Animal>to the class declaration; and - Implement a
int compareTo( Animal a )method to perform the comparisons.
Like this:
public class Animal implements Comparable<Animal>{
public String name;
public int year_discovered;
public String population;
public Animal(String name, int year_discovered, String population){
this.name = name;
this.year_discovered = year_discovered;
this.population = population;
}
public String toString(){
String s = "Animal name: "+ name+"\nYear Discovered: "+year_discovered+"\nPopulation: "+population;
return s;
}
@Override
public int compareTo( final Animal o) {
return Integer.compare(this.year_discovered, o.year_discovered);
}
}
Let's get right to the heart of the matter. Imagine we have a class K that we've defined as implementing Comparable<K>, and two references a and b to objects of class K.
To know if a is "less thanb`, we write
if (a.compareTo(b) < 0) {
// compareTo returned a negative number
,,,
}
To know if a is "equal to" b, we write
if (a.compareTo(b) == 0) {
// compareTo returned zero
...
}
And to know if a is "greater than" b, we write
if (a.compareTo(b) > 0) {
// compareTo returned a positive integer
...
}
Does that clear things up a bit?
Sorting list of Custom Objects in ascending and descending Order
Java provides two interfaces to sort objects using data members of the class:
Comparable and Comparator interfaces
Java Comparable interface
Java Comparable interface is used to order the objects of user-defined class. This interface is found in java.lang package and contains only one method named compareTo(Object). It provide single sorting sequence only i.e. you can sort the elements on based on single data member only. public int compareTo(Object obj): is used to compare the current object with the specified object. We can sort the elements of: String objects Wrapper class objects User-defined class objects
Java Comparator interface
Java Comparator interface is used to order the objects of user-defined class. Collections class provides static methods for sorting the elements of collection. Method of Collections class for sorting List elements public void sort(List list, Comparator c): is used to sort the elements of List by the given Comparator.
References: http://corejavapractical.blogspot.in/2017/08/algorithms-in-java.html
package com.mycompany.projectname.corejava;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
import java.util.concurrent.CopyOnWriteArrayList;
public class AlgorithmsDemo {
public static void main(String[] args) {
//sortingCustomObjectsByComparable();
sortingCustomObjectsByComparator();
}
private static void sortingCustomObjectsByComparable(){
// Sort Projects by project id in ascending order.
List projects = new ArrayList<>();
Project project = new Project();
project.setProjectId(100);
project.setProjectName("project 100");
projects.add(project);
Project project2 = new Project();
project2.setProjectId(200);
project2.setProjectName("project 200");
projects.add(project2);
Project project3 = new Project();
project3.setProjectId(50);
project3.setProjectName("project 50");
projects.add(project3);
Collections.sort(projects);
printList(projects);
}
private static void sortingCustomObjectsByComparator(){
// Sort Projects by project id in ascending order.
List projects = new ArrayList<>();
Project project = new Project();
project.setProjectId(100);
project.setProjectName("project 100");
projects.add(project);
Project project2 = new Project();
project2.setProjectId(200);
project2.setProjectName("project 200");
projects.add(project2);
Project project3 = new Project();
project3.setProjectId(50);
project3.setProjectName("project 50");
projects.add(project3);
// Sorting project by project id in ascending order in Java
Collections.sort(projects);
printList(projects);
// Sorting project by project id in descending order in Java
Collections.sort(projects, Collections.reverseOrder());
printList(projects);
// Sorting project by project name in ascending order in Java
Comparator comparator = new Comparator() {
@Override
public int compare(Project o1, Project o2) {
// TODO Auto-generated method stub
return o1.getProjectName().compareTo(o2.getProjectName());
}
};
Collections.sort(projects, comparator);
printList(projects);
}
private static void printList(List projects){
for(Project project : projects){
System.out.println(project.getProjectId());
System.out.println(project.getProjectName());
}
}
}
class Project implements Comparable{
private int projectId;
private String projectName;
public int getProjectId() {
return projectId;
}
public void setProjectId(int projectId) {
this.projectId = projectId;
}
public String getProjectName() {
return projectName;
}
public void setProjectName(String projectName) {
this.projectName = projectName;
}
@Override
public int compareTo(Project o) {
// TODO Auto-generated method stub
return this.projectId - o.getProjectId();
}
}