使用JAVA进行排序

时间:2023-02-02 04:51:59

利用JAVA完成排序

  当我们在进行数据库进行查询的时候,当需要按某个字段来进行排序的时候,可以使用SQL语句来完成排序,可以升序,也可以降序。JAVA中的Collections类也可以完成这种操作,SQL的用法和这种集合工具类的操作,可以用于不同的情况中,至于怎么用,可以根据业务场景来选择,下面我罗列出具体的代码,大家可以参考。

1)创建工程名为sort的JAVA工程

创建包结构:使用JAVA进行排序

创建User类:

 package com.sunyard.bean;

 /**
* <p>
* 用户类,实现Comparable接口,并且在实体类中实现需要排序的逻辑代码
* </p>
* @author:774346810@qq.com
* @date:2017-6-25
*/
public class User implements Comparable<User>{
private String id;//编号
private String name;//姓名
private double salary;//工资
private int age;//年龄
private String city;//城市 public User(){} public User(String id, String name, double salary, int age, String city) {
this.id = id;
this.name = name;
this.salary = salary;
this.age = age;
this.city = city;
} 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 double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
} @Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", salary=" + salary
+ ", age=" + age + ", city=" + city + "]";
}
@Override
public int compareTo(User o) {
int result = ((this.getAge() - o.getAge()) * 10);//按照年龄的降序排序
//int result = ((o.getAge() - this.getAge()) * 10);//按照年龄的升序排序
//int result = o.getName().compareTo(this.getName());//按照英文字典顺序的降序
//int result = this.getName().compareTo(o.getName());//按照英文字典顺序的升序
return result;
} }

创建测试类测试:

 package com.sunyard.test;

 import java.util.ArrayList;
import java.util.Collections;
import java.util.List; import com.sunyard.bean.User; public class Test {
public static void main(String[] args) {
List<User> users = new ArrayList<User>();
User user1 = new User("1", "zhangsan", 10000, 29, "hangzhou");
User user2 = new User("2", "lisi", 12000, 25, "hangzhou");
User user3 = new User("3", "wangwu", 11000, 27, "hangzhou");
User user4 = new User("4", "zhaoliu", 11500, 26, "hangzhou"); users.add(user1);
users.add(user2);
users.add(user3);
users.add(user4); System.out.println("排序前 -------------------- >>>");
for(int i = 0;i < users.size();i++){
System.out.println(users.get(i));
} Collections.sort(users); System.out.println("排序后 -------------------- >>>");
for(int i = 0;i < users.size();i++){
System.out.println(users.get(i));
}
}
}

控制台输出:

 排序前 -------------------- >>>
User [id=1, name=zhangsan, salary=10000.0, age=29, city=hangzhou]
User [id=2, name=lisi, salary=12000.0, age=25, city=hangzhou]
User [id=3, name=wangwu, salary=11000.0, age=27, city=hangzhou]
User [id=4, name=zhaoliu, salary=11500.0, age=26, city=hangzhou]
排序后 -------------------- >>>
User [id=2, name=lisi, salary=12000.0, age=25, city=hangzhou]
User [id=4, name=zhaoliu, salary=11500.0, age=26, city=hangzhou]
User [id=3, name=wangwu, salary=11000.0, age=27, city=hangzhou]
User [id=1, name=zhangsan, salary=10000.0, age=29, city=hangzhou]

第二种方式,实现自定义的比较器:

自定义比较器代码:

 package com.sunyard.util;

 import java.util.Comparator;

 import com.sunyard.bean.User;

 /**
* <p>实现自定义比较器</p>
* @author:774346810@qq.com
* @date:2017-6-25
*/
public class UserComparator implements Comparator<User>{ @Override
public int compare(User o1, User o2) {
int result = 0;
result = ((o1.getAge() - o2.getAge()) * 10);//升序
result = ((o2.getAge() - o1.getAge()) * 10);//降序
return result;
} }

测试类:

 package com.sunyard.test;

 import java.util.ArrayList;
import java.util.Collections;
import java.util.List; import com.sunyard.bean.User;
import com.sunyard.util.UserComparator; public class Demo {
public static void main(String[] args) {
List<User> users = new ArrayList<User>();
User user1 = new User("1", "zhangsan", 10000, 29, "hangzhou");
User user2 = new User("2", "lisi", 12000, 25, "hangzhou");
User user3 = new User("3", "wangwu", 11000, 27, "hangzhou");
User user4 = new User("4", "zhaoliu", 11500, 26, "hangzhou"); users.add(user1);
users.add(user2);
users.add(user3);
users.add(user4); System.out.println("排序前 -------------------- >>>");
for(int i = 0;i < users.size();i++){
System.out.println(users.get(i));
} UserComparator comparator = new UserComparator();
Collections.sort(users, comparator); System.out.println("排序后 -------------------- >>>");
for(int i = 0;i < users.size();i++){
System.out.println(users.get(i));
}
}
}

控制台输出:

 排序前 -------------------- >>>
User [id=1, name=zhangsan, salary=10000.0, age=29, city=hangzhou]
User [id=2, name=lisi, salary=12000.0, age=25, city=hangzhou]
User [id=3, name=wangwu, salary=11000.0, age=27, city=hangzhou]
User [id=4, name=zhaoliu, salary=11500.0, age=26, city=hangzhou]
排序后 -------------------- >>>
User [id=1, name=zhangsan, salary=10000.0, age=29, city=hangzhou]
User [id=3, name=wangwu, salary=11000.0, age=27, city=hangzhou]
User [id=4, name=zhaoliu, salary=11500.0, age=26, city=hangzhou]
User [id=2, name=lisi, salary=12000.0, age=25, city=hangzhou]