Brief introduction
Implement a generic sorting tools, sorted according to the sort of objects selected by a user; e.g., by use of age Age sort sorting, sorting by the wages or salary case of equal
Code
FIG code structure:
package cn.test.logan.day08; import java.util.ArrayList; public class SortUtilTest { public static void main(String[] args) { User u1 = new User("001","张三",30,1000); User u2 = new User("002","李四",29,1200); User u3 = new User("003","王五",27,1000); User u4 = new User("004","狗蛋",24,1400); ArrayList<User> list = new ArrayList<>(); list.add(u1); list.add(u2); list.add(u3); list.add(u4); // age sort // SortUtil.sort (List, new new AgeCompare ()); // According to the sort of wages // SortUtil.sort (List, new new SalaryCompare ()); // first based on salary, wages are equal according to age SortUtil.sort (List, new new SalaryAgeCompare ()); for(User u:list) { System.out.println(u); } } }
package cn.test.logan.day08; import java.util.ArrayList; public class SortUtil { public static void sort(ArrayList<User> uList,Compare com) { for(int i=0;i<uList.size()-1;i++) { for(int j=0;j<uList.size()-1-i;j++) { // 判断大小 boolean res = com.compare(uList.get(j), uList.get(j+1)); if(res) { User tmp = uList.get(j); uList.set(j,uList.get(j+1)); uList.set(j+1, tmp); } } } } }
package cn.test.logan.day08; public interface Compare { public boolean compare(User u1,User u2); }
package cn.test.logan.day08; /** * Define a User class * Include: ID, name, age, salary * @author QIN * */ public class User { private String id; private String name; private int age; private float salary; public User() { } public User(String id, String name, int age, float salary) { this.id = id; this.name = name; this.age = age; this.salary = salary; } public String getId() { return id; } public void setId(String id) { this.id = id; } 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 float getSalary() { return salary; } public void setSalary(float salary) { this.salary = salary; } // 定义toString方法 @Override public String toString() { return this.id + "," + this.name + "," + this.age + "," + this.salary; } }
The following are the three after age, salary, age, sort of like the first wage
package cn.test.logan.day08; public class AgeCompare implements Compare{ @Override public boolean compare(User u1, User u2) { if(u1.getAge() > u2.getAge()) { return true; } return false; } }
package cn.test.logan.day08; public class SalaryCompare implements Compare{ @Override public boolean compare(User u1, User u2) { if(u1.getSalary() > u2.getSalary()) { return true; } return false; } }
package cn.test.logan.day08; public class SalaryAgeCompare implements Compare{ @Override public Boolean Compare (the User U1, the User U2) { // first sorted according to wages, salaries if they are the same age ordered IF (u1.getSalary ()> u2.getSalary ()) { return to true ; } if(u1.getSalary() == u2.getSalary()) { if(u1.getAge() > u2.getAge() ) { return true; } } return false; } }