码迷,mamicode.com
首页 > 编程语言 > 详细

Arrays.sort()方法给出了,只是该类实现了comparable方法,那就可以实现排序

时间:2019-12-16 22:42:49      阅读:102      评论:0      收藏:0      [点我收藏+]

标签:rgs   ring   compareto   性能   nts   this   class   pareto   override   

package 测试Arrays排序功能的强大性能;

import java.util.Arrays;

public class Ceshi {
    public static void main(String[] args) {
        Employee[] employees = new Employee[3];
        employees[0] = new Employee("lgq", 2309.98);
        employees[1] = new Employee("zcp", 234232.8);
        employees[2] = new Employee("wyl", 643.98);
        System.out.println(Arrays.deepToString(employees));
        Arrays.sort(employees);
        System.out.println(Arrays.deepToString(employees));
    }
}

class Employee implements Comparable<Employee> {

    String name;
    double salary;

    public Employee(String name, double salary) {
        this.name = name;
        this.salary = salary;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getSalary() {
        return salary;
    }

    public void setSalary(int salary) {
        this.salary = salary;
    }

    public void raiseSalary(double byPercent) {
        double raise = salary * byPercent / 100;
        salary += raise;
    }

    @Override
    public String toString() {
        return "{" + this.name + ":" + this.salary + "}" ;
    }

    @Override
    public int compareTo(Employee o) {

        if (this.salary < o.salary) return -1;
        if (this.salary > o.salary) return 1;

        return 0;
    }
}

Arrays.sort()方法给出了,只是该类实现了comparable方法,那就可以实现排序

标签:rgs   ring   compareto   性能   nts   this   class   pareto   override   

原文地址:https://www.cnblogs.com/sharysea/p/12051403.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!