一、实现Comparable接口(用于类之间的排序):
假设有Employee类,有name和salary字段,
需要实现Comparable<T>接口:
-
public
class Employee implements Comparable<Employee> {
-
private String name;
-
private
double salary;
-
-
public Employee() {
-
}
-
-
public Employee(String name, double salary) {
-
this.name = name;
-
this.salary = salary;
-
}
-
-
public void raiseSalary(double byPercent) {
-
double raise = salary * byPercent /
100;
-
salary += raise;
-
}
-
-
/*
-
* Compares employees by salary
-
* @param other another Employee object
-
* return a negative value if this employee has a lower salary than
-
* otherObject , 0 if the salaries are the same, a positive value otherwise
-
*/
-
public int compareTo(Employee other) {
-
return Double.compare(salary, other.salary);
-
}
-
-
public String getName() {
-
return name;
-
}
-
-
public void setName(String name) {
-
this.name = name;
-
}
-
-
public double getSalary() {
-
return salary;
-
}
-
-
public void setSalary(double salary) {
-
this.salary = salary;
-
}
-
}
假设希望根据雇员的薪水进行比较,要实现compareTo方法:
-
public int compareTo(Object otherObject)
-
{
-
Employee other = (Employee) otherObject;
-
return Double.compare(salary, other.salary);
-
}
我们来测试一下,这个比较排序是否能成功:
-
-
import java.util.Arrays;
-
-
-
public
class Test {
-
public static void main(String[] args) {
-
Employee[] staff =
new Employee[
5];
-
staff[
0] =
new Employee(
"Harry Hacker",
35000);
-
staff[
1] =
new Employee(
"Carl Cracker",
75000);
-
staff[
2] =
new Employee(
"Tony Tester",
38000);
-
staff[
3] =
new Employee(
"Tony Bool",
48000);
-
staff[
4] =
new Employee(
"June Bo",
48001);
-
Arrays.sort(staff);
-
// print out information about all Employee objects
-
for (Employee e : staff) {
-
System.out.println(
"name=" + e.getName() +
" , salary=" + e.getSalary());
-
}
-
}
-
}
输出结果为:
-
name=Harry Hacker , salary=
35000.0
-
name=Tony Tester , salary=
38000.0
-
name=Tony Bool , salary=
48000.0
-
name=June Bo , salary=
48001.0
-
name=Carl Cracker , salary=
75000.0
排序是可以的。
所以,排序可以实现Comparable接口,然后自定义compareTo方法即可(因为sort方法要有提供对象比较的方式)。
二、使用比较器(comparator)作为sort的参数(用于单个类型的排序):
比较器实现了Comparator接口
如: 需要按照字符的长度递增来进行排序:
-
-
import java.util.Arrays;
-
import java.util.Comparator;
-
-
public
class Test {
-
public static void main(String[] args) {
-
-
String[] friends = {
"Peter",
"Paulllll",
"Mary" };
-
Arrays.sort(friends,
new LengthComparator());
-
for (String f : friends) {
-
System.out.print(f +
" ");
-
}
-
}
-
-
static
class LengthComparator implements Comparator<String>
-
{
-
public int compare(String first, String second) {
-
return first.length()- second.length();
-
}
-
}
-
-
}
输出结果为:
Mary Peter Paulllll
排序按字符长度递增顺序。
如果只是需要按照字符的字典顺序排序的话,则不需要实现Comparator:
-
public
class Test {
-
public static void main(String[] args) {
-
String[] friends = {
"Peter",
"Paulllll",
"Mary",
"ziqizh" ,
"yoyo"};
-
Arrays.sort(friends);
-
for (String f : friends) {
-
System.out.print(f +
" ");
-
}
-
}
-
-
static
class LengthComparator implements Comparator<String>
-
{
-
public int compare(String first, String second) {
-
return first.length()- second.length();
-
}
-
}
-
-
}
输出结果为:
Mary Paulllll Peter yoyo ziqizh
C++也有类似的排序,单个类型或者是结构体都可以,自己写一个比较函数cmp,作为sort的参数排序即可。
Java内容具体可以参照《Java核心技术卷1》6.2章节