Java Comparator has two types: Comparable interface and Comparator interface.
When sorting an array of objects, the comparator plays an obvious role. First, we will explain the Comparable interface.
Implement the Comparable interface for the objects to be sorted, override the compareTo (T o) method, and define the sorting rules in it, then you can directly call java. util. arrays. sort () to sort the object array. The example is as follows:
Copy codeThe Code is as follows: class Student implements Comparable <Student> {
Private String name;
Private int age;
Private float score;
Public Student (String name, int age, float score ){
This. name = name;
This. age = age;
This. score = score;
}
Public String toString ()
{
Return name + "\ t" + age + "\ t" + score;
}
@ Override
Public int compareTo (Student o ){
// TODO Auto-generated method stub
If (this. score> o. score) // The score is private. why can it be called directly? this is because it is inside the Student class.
Return-1; // sort by height to the end
Else if (this. score <o. score)
Return 1;
Else {
If (this. age> o. age)
Return 1; // sorted from bottom to high
Else if (this. age <o. age)
Return-1;
Else
Return 0;
}
}
}
Public class ComparableDemo01 {
/**
* @ Param args
*/
Public static void main (String [] args ){
// TODO Auto-generated method stub
Student stu [] = {new Student ("zhangsan", 20, 90.0f ),
New Student ("lisi", 22, 90.0f ),
New Student ("wangwu", 20, 99366f ),
New Student ("sunliu", 22,100.0 f )};
Java. util. Arrays. sort (stu );
For (Student s: stu)
{
System. out. println (s );
}
}
}
Program running result:
Sunliu 22 100.0
Wangwu 20 99.0
Zhangsan 20 90.0
Lisi 22, 90.0
However, when designing a class, we often do not consider implementing the Comparable interface for the class, so we need to use another Comparator interface Comparator.
From the above example, we can find that compareTo (T o) has only one parameter, and the compare (T o1, T o2) that must be implemented in the Comparator interface has two parameters.
Code example:Copy codeThe Code is as follows: package edu. sjtu. ist. comutil;
Import java. util. Comparator;
Class Student {
Private String name;
Private int age;
Private float score;
Public Student (String name, int age, float score ){
This. name = name;
This. age = age;
This. score = score;
}
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 float getScore (){
Return score;
}
Public void setScore (float score ){
This. score = score;
}
Public String toString ()
{
Return name + "\ t" + age + "\ t" + score;
}
}
Class StudentComparator implements Comparator <Student> {
@ Override
Public int compare (Student o1, Student o2 ){
// TODO Auto-generated method stub
If (o1.getScore ()> o2.getScore ())
Return-1;
Else if (o1.getScore () <o2.getScore ())
Return 1;
Else {
If (o1.getAge ()> o2.getAge ())
Return 1;
Else if (o1.getAge () <o2.getAge ())
Return-1;
Else
Return 0;
}
}
}
Public class ComparableDemo02 {
/**
* @ Param args
*/
Public static void main (String [] args ){
// TODO Auto-generated method stub
Student stu [] = {new Student ("zhangsan", 20, 90.0f ),
New Student ("lisi", 22, 90.0f ),
New Student ("wangwu", 20, 99366f ),
New Student ("sunliu", 22,100.0 f )};
Java. util. Arrays. sort (stu, new StudentComparator ());
For (Student s: stu)
{
System. out. println (s );
}
}
}