首页 > 代码库 > 使用JAVA进行排序
使用JAVA进行排序
利用JAVA完成排序
当我们在进行数据库进行查询的时候,当需要按某个字段来进行排序的时候,可以使用SQL语句来完成排序,可以升序,也可以降序。JAVA中的Collections类也可以完成这种操作,SQL的用法和这种集合工具类的操作,可以用于不同的情况中,至于怎么用,可以根据业务场景来选择,下面我罗列出具体的代码,大家可以参考。
1)创建工程名为sort的JAVA工程
创建包结构:
创建User类:
1 package com.sunyard.bean; 2 3 /** 4 * <p> 5 * 用户类,实现Comparable接口,并且在实体类中实现需要排序的逻辑代码 6 * </p> 7 * @author:774346810@qq.com 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 public User(){} 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 return id; 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:774346810@qq.com 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进行排序
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。