使用JAVA進行排序
阿新 • • 發佈:2017-06-25
創建工程 .so pack mage sets ray alt 需要 ring
利用JAVA完成排序
當我們在進行數據庫進行查詢的時候,當需要按某個字段來進行排序的時候,可以使用SQL語句來完成排序,可以升序,也可以降序。JAVA中的Collections類也可以完成這種操作,SQL的用法和這種集合工具類的操作,可以用於不同的情況中,至於怎麽用,可以根據業務場景來選擇,下面我羅列出具體的代碼,大家可以參考。
1)創建工程名為sort的JAVA工程
創建包結構:
創建User類:
1 package com.sunyard.bean; 2 3 /** 4 * <p> 5 * 用戶類,實現Comparable接口,並且在實體類中實現需要排序的邏輯代碼6 * </p> 7 * @author:[email protected] 8 * @date:2017-6-25 9 */ 10 public class User implements Comparable<User>{ 11 private String id;//編號 12 private String name;//姓名 13 private double salary;//工資 14 private int age;//年齡 15 private String city;//城市 16 17 publicUser(){} 18 19 public User(String id, String name, double salary, int age, String city) { 20 this.id = id; 21 this.name = name; 22 this.salary = salary; 23 this.age = age; 24 this.city = city; 25 } 26 27 public String getId() { 28 returnid; 29 } 30 public void setId(String id) { 31 this.id = id; 32 } 33 public String getName() { 34 return name; 35 } 36 public void setName(String name) { 37 this.name = name; 38 } 39 public double getSalary() { 40 return salary; 41 } 42 public void setSalary(double salary) { 43 this.salary = salary; 44 } 45 public int getAge() { 46 return age; 47 } 48 public void setAge(int age) { 49 this.age = age; 50 } 51 public String getCity() { 52 return city; 53 } 54 public void setCity(String city) { 55 this.city = city; 56 } 57 58 @Override 59 public String toString() { 60 return "User [id=" + id + ", name=" + name + ", salary=" + salary 61 + ", age=" + age + ", city=" + city + "]"; 62 } 63 @Override 64 public int compareTo(User o) { 65 int result = ((this.getAge() - o.getAge()) * 10);//按照年齡的降序排序 66 //int result = ((o.getAge() - this.getAge()) * 10);//按照年齡的升序排序 67 //int result = o.getName().compareTo(this.getName());//按照英文字典順序的降序 68 //int result = this.getName().compareTo(o.getName());//按照英文字典順序的升序 69 return result; 70 } 71 72 }
創建測試類測試:
1 package com.sunyard.test; 2 3 import java.util.ArrayList; 4 import java.util.Collections; 5 import java.util.List; 6 7 import com.sunyard.bean.User; 8 9 public class Test { 10 public static void main(String[] args) { 11 List<User> users = new ArrayList<User>(); 12 User user1 = new User("1", "zhangsan", 10000, 29, "hangzhou"); 13 User user2 = new User("2", "lisi", 12000, 25, "hangzhou"); 14 User user3 = new User("3", "wangwu", 11000, 27, "hangzhou"); 15 User user4 = new User("4", "zhaoliu", 11500, 26, "hangzhou"); 16 17 users.add(user1); 18 users.add(user2); 19 users.add(user3); 20 users.add(user4); 21 22 System.out.println("排序前 -------------------- >>>"); 23 for(int i = 0;i < users.size();i++){ 24 System.out.println(users.get(i)); 25 } 26 27 Collections.sort(users); 28 29 System.out.println("排序後 -------------------- >>>"); 30 for(int i = 0;i < users.size();i++){ 31 System.out.println(users.get(i)); 32 } 33 } 34 }
控制臺輸出:
1 排序前 -------------------- >>> 2 User [id=1, name=zhangsan, salary=10000.0, age=29, city=hangzhou] 3 User [id=2, name=lisi, salary=12000.0, age=25, city=hangzhou] 4 User [id=3, name=wangwu, salary=11000.0, age=27, city=hangzhou] 5 User [id=4, name=zhaoliu, salary=11500.0, age=26, city=hangzhou] 6 排序後 -------------------- >>> 7 User [id=2, name=lisi, salary=12000.0, age=25, city=hangzhou] 8 User [id=4, name=zhaoliu, salary=11500.0, age=26, city=hangzhou] 9 User [id=3, name=wangwu, salary=11000.0, age=27, city=hangzhou] 10 User [id=1, name=zhangsan, salary=10000.0, age=29, city=hangzhou]
第二種方式,實現自定義的比較器:
自定義比較器代碼:
1 package com.sunyard.util; 2 3 import java.util.Comparator; 4 5 import com.sunyard.bean.User; 6 7 /** 8 * <p>實現自定義比較器</p> 9 * @author:[email protected] 10 * @date:2017-6-25 11 */ 12 public class UserComparator implements Comparator<User>{ 13 14 @Override 15 public int compare(User o1, User o2) { 16 int result = 0; 17 result = ((o1.getAge() - o2.getAge()) * 10);//升序 18 result = ((o2.getAge() - o1.getAge()) * 10);//降序 19 return result; 20 } 21 22 }
測試類:
1 package com.sunyard.test; 2 3 import java.util.ArrayList; 4 import java.util.Collections; 5 import java.util.List; 6 7 import com.sunyard.bean.User; 8 import com.sunyard.util.UserComparator; 9 10 public class Demo { 11 public static void main(String[] args) { 12 List<User> users = new ArrayList<User>(); 13 User user1 = new User("1", "zhangsan", 10000, 29, "hangzhou"); 14 User user2 = new User("2", "lisi", 12000, 25, "hangzhou"); 15 User user3 = new User("3", "wangwu", 11000, 27, "hangzhou"); 16 User user4 = new User("4", "zhaoliu", 11500, 26, "hangzhou"); 17 18 users.add(user1); 19 users.add(user2); 20 users.add(user3); 21 users.add(user4); 22 23 System.out.println("排序前 -------------------- >>>"); 24 for(int i = 0;i < users.size();i++){ 25 System.out.println(users.get(i)); 26 } 27 28 UserComparator comparator = new UserComparator(); 29 Collections.sort(users, comparator); 30 31 System.out.println("排序後 -------------------- >>>"); 32 for(int i = 0;i < users.size();i++){ 33 System.out.println(users.get(i)); 34 } 35 } 36 }
控制臺輸出:
1 排序前 -------------------- >>> 2 User [id=1, name=zhangsan, salary=10000.0, age=29, city=hangzhou] 3 User [id=2, name=lisi, salary=12000.0, age=25, city=hangzhou] 4 User [id=3, name=wangwu, salary=11000.0, age=27, city=hangzhou] 5 User [id=4, name=zhaoliu, salary=11500.0, age=26, city=hangzhou] 6 排序後 -------------------- >>> 7 User [id=1, name=zhangsan, salary=10000.0, age=29, city=hangzhou] 8 User [id=3, name=wangwu, salary=11000.0, age=27, city=hangzhou] 9 User [id=4, name=zhaoliu, salary=11500.0, age=26, city=hangzhou] 10 User [id=2, name=lisi, salary=12000.0, age=25, city=hangzhou]
使用JAVA進行排序