一、题目要求
根据图中rise作为参数对(学校人数)索引进行排序。(①升序:由小到大 ②降序:由大到小)
(注:
rise
是整数型,1是升序,由小到大;0是降序,由大到小)
二、解题代码
StundentBean.java类
public class StudentBean { // 学生名字 private String sname; public String getSname() { return sname; } public void setSname(String sname) { this.sname = sname; } }
SchoolVO.java类
import java.util.List; public class SchoolVO { // 学校名字 private String name; // 学校历史 private String history; // 学校年龄 private Integer age; // 学生总数 private Integer sum; // 学校具体信息(班级、学生等信息),?:泛型(可以是实体类,Map等) private List<?> list; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getHistory() { return history; } public void setHistory(String history) { this.history = history; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public List<?> getList() { return list; } public void setList(List<?> list) { this.list = list; } public Integer getSum() { return sum; } public void setSum(Integer sum) { this.sum = sum; } }
测试类(方法类)
@GetMapping("/test") public Map<String, Object> test(Integer rise) { Map<String, Object> map = new HashMap<String, Object>(); List<SchoolVO> list = new ArrayList<>(); // 第一中学的情况 List<StudentBean> students_1 = new ArrayList<>(); StudentBean xiaoming = new StudentBean(); StudentBean xiaojun = new StudentBean(); StudentBean xiaohong = new StudentBean(); xiaoming.setSname("小明"); xiaojun.setSname("小军"); xiaohong.setSname("小红"); students_1.add(xiaoming); students_1.add(xiaojun); students_1.add(xiaohong); SchoolVO school_1 = new SchoolVO(); school_1.setName("第一中学"); school_1.setAge(4); school_1.setHistory("第一中学,历史悠久,诞生于2017年"); school_1.setSum(students_1.size()); school_1.setList(students_1); // 第二中学的情况 List<StudentBean> students_2 = new ArrayList<>(); StudentBean xiaohei1 = new StudentBean(); StudentBean xiaobai1 = new StudentBean(); StudentBean xiaohei2 = new StudentBean(); StudentBean xiaobai2 = new StudentBean(); xiaohei1.setSname("小黑1"); xiaobai1.setSname("小白1"); xiaohei2.setSname("小黑2"); xiaobai2.setSname("小白2"); students_2.add(xiaohei1); students_2.add(xiaobai1); students_2.add(xiaohei2); students_2.add(xiaobai2); SchoolVO school_2 = new SchoolVO(); school_2.setName("第二中学"); school_2.setAge(3); school_2.setHistory("第二中学,历史悠久,诞生于2018年"); school_2.setSum(students_2.size()); school_2.setList(students_2); // 第三中学的情况 List<StudentBean> students_3 = new ArrayList<>(); StudentBean xiaoxiao = new StudentBean(); StudentBean xiaotian = new StudentBean(); xiaoxiao.setSname("小小"); xiaotian.setSname("小天"); students_3.add(xiaoxiao); students_3.add(xiaotian); SchoolVO school_3 = new SchoolVO(); school_3.setName("第三中学"); school_3.setAge(3); school_3.setHistory("第三中学,历史悠久,诞生于2018年"); school_3.setSum(students_3.size()); school_3.setList(students_3); // 把三个中学放一起 list.add(school_1); list.add(school_2); list.add(school_3); // ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓排序功能(核心代码)↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓ if (rise != null && rise == 1) { Collections.sort(list, new Comparator<SchoolVO>() { @Override public int compare(SchoolVO o1, SchoolVO o2) { return o1.getSum() - o2.getSum(); } }); } else if (rise != null && rise == 0) { Collections.sort(list, new Comparator<SchoolVO>() { @Override public int compare(SchoolVO o1, SchoolVO o2) { return o2.getSum() - o1.getSum(); } }); } // ↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑排序功能(核心代码)↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑ map.put("data", list); map.put("flag", 1); map.put("msg", "查询成功"); return map; }
三、测试结果
升序
此时,参数rise为1,发起请求,结果如下:
降序
此时,参数rise为0,发起请求,结果如下:
四、核心 Collections.sort 讲解
Collections.sort讲解
Collections.sort
方法底层其实就是调用Arrays.sort
方法。
Arrays.sort
的排序方法是快排(快速排序)和优化的归并排序。
其中,快排是针对基础类型;Object(对象)类型是针对归并排序。
Collections.sort参数情况
new Comparator<Integer>()对象新重写的compare方法
返回值(return)是
负数为倒序
,正数为正序
。
【最后】
感谢你看到最后,如果你持有不同的看法,欢迎你在文章下方进行留言、评论。
我是南方者,一个热爱计算机更热爱祖国的南方人。