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

一、实现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章节

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值