There are two comparators in Java: Comparable and Comparator
Integer, Double, etc., can be compared directly because they are already compared. However, there are often cases where we need to sort collections. In this case, we need to manually define a Java comparator to tell the program how two objects compare sizes.
Comparable
Comparable is called an internal comparator because we create the class that needs to be sorted and we implement that class, and we manually specify the sort at the beginning of creation.
Implement this class, and then we need to override its compareTo method, passing in a class that returns an int compared to the current class.
The comparison rules are: if it is passed in > 0 and returns a positive number, it is sorted in ascending order; if it returns a negative number, it is sorted in descending order. You can also view return a-b as ascending and return b-a as descending.
If you call compare greater than 0, you swap the previous number with the next number, so you put the larger number behind you, which is called ascending order. If it’s less than or equal to 0, then you’re not going to swap.
import java.util.*;
public class Test {
public static void main(String[] args) {
MySort m1 = new MySort();
MySort m2 = new MySort();
m1.setId(10);
m2.setId(8);
LinkedList<MySort> list = new LinkedList<>();
list.add(m1);
list.add(m2);
System.out.println(list.get(0).getId() + "");
System.out.println(list.get(1).getId());
Collections.sort(list);
System.out.println(list.get(0).getId() + "");
System.out.println(list.get(1).getId());
}
class MySort implements Comparable<MySort> {
private String name;
private int id;
public void setName(String name) {
this.name = name;
}
public String getName(a) {
return name;
}
public void setId(int id) {
this.id = id;
}
public int getId(a) {
return id;
}
@Override
public int compareTo(MySort o) {
// Sort in ascending order, low -> high
if (id - o.id > 0) {
return 1;
} else if (id - o.id == 0) {
return 0;
} else {
return -1; }}}Copy the code
Comparator
Comparators are called external comparators because they can be separated from classes that need to be compared. For example, a Comparator can be in a List that does not implement Comparable. You can pass in a Comparator in collections.sort () and override its compare method.
import java.util.*;
public class Test {
public static void main(String[] args) {
MySort m1 = new MySort();
MySort m2 = new MySort();
m1.setId(10);
m2.setId(8);
LinkedList<MySort> list = new LinkedList<>();
list.add(m1);
list.add(m2);
System.out.print(list.get(0).getId() + "");
System.out.println(list.get(1).getId());
Collections.sort(list, new Comparator<MySort>() {
@Override
public int compare(MySort o1, MySort o2) {
if (o1.getId() - o2.getId() > 0) {
return 1;
} else if (o1.getId() - o2.getId() == 0) {
return 0;
} else {
return -1; }}}); System.out.print(list.get(0).getId() + "");
System.out.println(list.get(1).getId()); }}class MySort {
private String name;
private int id;
public void setName(String name) {
this.name = name;
}
public String getName(a) {
return name;
}
public void setId(int id) {
this.id = id;
}
public int getId(a) {
returnid; }}Copy the code