ArrayList排序
package LESSON11; import java.util.ArrayList; class Employee{ private String name; private int age; private double salary; public Employee(String name,int age,double salary) { this.name=name; this.age=age; this.salary=salary; } 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 double getSalary() { return salary; } public void setSalary(double salary) { this.salary = salary; } } public class exercise2 { /** 定义一个Employee类,属性: name:String,age:int,salary:double 把若干Employee对象放在List中,排序并遍历输出,排序规则:salary高的在前面, salary相同时age大的在前面,age也相同时按照name的字典顺序排列(a-z排列) */ public static void main(String[] args) { Employee emp1=new Employee("jack", 20, 3000); Employee emp2=new Employee("jerry", 20, 3000); Employee emp3=new Employee("rose", 30, 5000); Employee emp4=new Employee("lucy", 40, 5000); Employee emp5=new Employee("tom", 30, 7000); ArrayList<Employee> list=new ArrayList<Employee>(); list.add(emp1); list.add(emp2); list.add(emp3); list.add(emp4); list.add(emp5); //冒泡 for (int i = 0; i < list.size()-1; i++) {//遍数 for (int j = 0; j < list.size()-1-i; j++) {//次数 Employee e1=list.get(j);//获取被比较的对象 Employee e2=list.get(j+1); if(e1.getSalary()<e2.getSalary()){//比较薪资 list.set(j, e2);//交换位置 list.set(j+1, e1); }else if(e1.getSalary()==e2.getSalary()){ if(e1.getAge()<e2.getAge()){//比较年龄 list.set(j, e2);//交换位置 list.set(j+1, e1); }else if(e1.getAge()==e2.getAge()){ if(e1.getName().compareTo(e2.getName())>0){//比较姓名 list.set(j, e2);//交换位置 list.set(j+1, e1); }
} } } } for (int i = 0; i < list.size(); i++) { System.out.println(list.get(i).getName()+"\t"+list.get(i).getAge()+"\t"+list.get(i).getSalary()); } } }
package LESSON11; import java.util.ArrayList; import java.util.Collections; class Employee implements Comparable<Employee>{ private String name; private int age; private double salary; public Employee(String name,int age,double salary) { this.name=name; this.age=age; this.salary=salary; } 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 double getSalary() { return salary; } public void setSalary(double salary) { this.salary = salary; } @Override public int compareTo(Employee o) { // this表示当前对象 if(o.salary==this.salary){ if(o.age==this.age){ return this.name.compareTo(o.name); } return o.age-this.age; } return (int)(o.salary-this.salary); } } public class exercise2 { /** 定义一个Employee类,属性: name:String,age:int,salary:double 把若干Employee对象放在List中,排序并遍历输出,排序规则:salary高的在前面, salary相同时age大的在前面,age也相同时按照name的字典顺序排列(a-z排列) */ public static void main(String[] args) { Employee emp1=new Employee("jerry", 20, 3000); Employee emp2=new Employee("jacky", 20, 3000); Employee emp3=new Employee("rose", 30, 5000); Employee emp4=new Employee("lucy", 40, 5000); Employee emp5=new Employee("tom", 30, 7000); ArrayList<Employee> list=new ArrayList<Employee>(); list.add(emp1); list.add(emp2); list.add(emp3); list.add(emp4); list.add(emp5); Collections.sort(list); for (int i = 0; i < list.size(); i++) { System.out.println(list.get(i).getName()+"\t"+list.get(i).getAge()+"\t"+list.get(i).getSalary()); } } }
运行结果