Java 实现对Arrays类 自定义 排序sort的2种方法

一、实现Comparable接口(用于类之间的排序):

 假设有Employee类,有name和salary字段,

需要实现Comparable<T>接口:


 
 
  1. public class Employee implements Comparable<Employee> {
  2. private String name;
  3. private double salary;
  4. public Employee() {
  5. }
  6. public Employee(String name, double salary) {
  7. this.name = name;
  8. this.salary = salary;
  9. }
  10. public void raiseSalary(double byPercent) {
  11. double raise = salary * byPercent / 100;
  12. salary += raise;
  13. }
  14. /*
  15. * Compares employees by salary
  16. * @param other another Employee object
  17. * return a negative value if this employee has a lower salary than
  18. * otherObject , 0 if the salaries are the same, a positive value otherwise
  19. */
  20. public int compareTo(Employee other) {
  21. return Double.compare(salary, other.salary);
  22. }
  23. public String getName() {
  24. return name;
  25. }
  26. public void setName(String name) {
  27. this.name = name;
  28. }
  29. public double getSalary() {
  30. return salary;
  31. }
  32. public void setSalary(double salary) {
  33. this.salary = salary;
  34. }
  35. }

假设希望根据雇员的薪水进行比较,要实现compareTo方法:


 
 
  1. public int compareTo(Object otherObject)
  2. {
  3. Employee other = (Employee) otherObject;
  4. return Double.compare(salary, other.salary);
  5. }

我们来测试一下,这个比较排序是否能成功:


 
 
  1. import java.util.Arrays;
  2. public class Test {
  3. public static void main(String[] args) {
  4. Employee[] staff = new Employee[ 5];
  5. staff[ 0] = new Employee( "Harry Hacker", 35000);
  6. staff[ 1] = new Employee( "Carl Cracker", 75000);
  7. staff[ 2] = new Employee( "Tony Tester", 38000);
  8. staff[ 3] = new Employee( "Tony Bool", 48000);
  9. staff[ 4] = new Employee( "June Bo", 48001);
  10. Arrays.sort(staff);
  11. // print out information about all Employee objects
  12. for (Employee e : staff) {
  13. System.out.println( "name=" + e.getName() + " , salary=" + e.getSalary());
  14. }
  15. }
  16. }

输出结果为:


 
 
  1. name=Harry Hacker , salary= 35000.0
  2. name=Tony Tester , salary= 38000.0
  3. name=Tony Bool , salary= 48000.0
  4. name=June Bo , salary= 48001.0
  5. name=Carl Cracker , salary= 75000.0

排序是可以的。

所以,排序可以实现Comparable接口,然后自定义compareTo方法即可(因为sort方法要有提供对象比较的方式)。

 

二、使用比较器(comparator)作为sort的参数(用于单个类型的排序):

比较器实现了Comparator接口

如: 需要按照字符的长度递增来进行排序:


 
 
  1. import java.util.Arrays;
  2. import java.util.Comparator;
  3. public class Test {
  4. public static void main(String[] args) {
  5. String[] friends = { "Peter", "Paulllll", "Mary" };
  6. Arrays.sort(friends, new LengthComparator());
  7. for (String f : friends) {
  8. System.out.print(f + " ");
  9. }
  10. }
  11. static class LengthComparator implements Comparator<String>
  12. {
  13. public int compare(String first, String second) {
  14. return first.length()- second.length();
  15. }
  16. }
  17. }

输出结果为:

Mary Peter Paulllll 
 
 

排序按字符长度递增顺序。

 

如果只是需要按照字符的字典顺序排序的话,则不需要实现Comparator:


 
 
  1. public class Test {
  2. public static void main(String[] args) {
  3. String[] friends = { "Peter", "Paulllll", "Mary", "ziqizh" , "yoyo"};
  4. Arrays.sort(friends);
  5. for (String f : friends) {
  6. System.out.print(f + " ");
  7. }
  8. }
  9. static class LengthComparator implements Comparator<String>
  10. {
  11. public int compare(String first, String second) {
  12. return first.length()- second.length();
  13. }
  14. }
  15. }

输出结果为:


 
 
  1. Mary Paulllll Peter yoyo ziqizh

 

C++也有类似的排序,单个类型或者是结构体都可以,自己写一个比较函数cmp,作为sort的参数排序即可。

 

Java内容具体可以参照《Java核心技术卷1》6.2章节

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值