在Java编程中,经常要对List集合进行排序,因此对List排序的各种情况进行总结,希望对各位读者有所帮助。
1.1按照变量属性升序,降序排序
/** * 单属性变量list 自身属性升序, 降序排序 */ private void singleVariableSort1(){ Listlist = Arrays.asList(10,1,6,4,8,7,9,3,2,5); System.out.println("原始数据:"); list.forEach(n ->{System.out.print(n+", ");}); System.out.println(""); System.out.println("升序排列:"); Collections.sort(list); // 升序排列 list.forEach(n ->{System.out.print(n+", ");}); System.out.println(""); System.out.println("降序排列:"); Collections.reverse(list); // 倒序排列 list.forEach(n ->{System.out.print(n+", ");}); }
1.2按照自定义的顺序排序
/** * 单属性变量list 自定义顺序排序 */ private void singleVariableSort2(){ Listlist = Arrays.asList("北京","上海","北京","广州","广州","上海","北京","上海"); List sortRule = Arrays.asList("北京","上海","广州"); System.out.println("原始数据:"); list.forEach(n ->{System.out.print(n+", ");}); System.out.println(""); System.out.println("排序规则:"); sortRule.forEach(n ->{System.out.print(n+", ");}); System.out.println(""); System.out.println("正序排列:"); Collections.sort(list, new Comparator () { public int compare(String a1, String a2) { int io1 = sortRule.indexOf(a1); int io2 = sortRule.indexOf(a2); return io1 - io2; } }); list.forEach(n ->{System.out.print(n+", ");}); System.out.println(""); System.out.println("倒序排列:"); Collections.sort(list, new Comparator() { public int compare(String a1, String a2) { int io1 = sortRule.indexOf(a1); int io2 = sortRule.indexOf(a2); return io2 - io1; } }); list.forEach(n ->{System.out.print(n+", ");}); }
2.1按照对象单属性升序,降序排序
/** * 对象单属性升序,降序排序 */ private void entitySort1(){ //Student 的 list 集合 Liststudents = new ArrayList<>(); students.add(new Student("张三",90,180,"电气学院","北京")); students.add(new Student("李四",80,165,"计算机学院","上海")); students.add(new Student("王五",91,170,"财经学院","上海")); students.add(new Student("赵明",80,182,"计算机学院","北京")); students.add(new Student("钱海",75,181,"计算机学院","广州")); students.add(new Student("孙理",82,172,"财经学院","上海")); students.add(new Student("周伟",90,168,"电气学院","广州")); students.add(new Student("郑亮",80,178,"财经学院","广州")); System.out.println("原始数据:"); students.forEach(s ->{ System.out.println(s.getName()+" "+ s.getScore()+" "+s.getHeight()+" "+ s.getCollege()+""+s.getAddress());}); System.out.println("按照分数升序排序:"); students.sort(comparing(Student::getScore)); students.forEach(s ->{ System.out.println(s.getName()+" "+ s.getScore()+" "+s.getHeight()+" "+ s.getCollege()+""+s.getAddress());}); System.out.println("按照分数降序排序:"); students.sort(comparing(Student::getScore).reversed()); students.forEach(s ->{ System.out.println(s.getName()+" "+ s.getScore()+" "+s.getHeight()+" "+s.getCollege()+""+s.getAddress());}); }
2.2按照对象多属性升序,降序排序
/** * 对象多属性升序,降序排序 */ private void entitySort2(){ //Student 的 list 集合 Liststudents = new ArrayList<>(); students.add(new Student("张三",90,180,"电气学院","北京")); students.add(new Student("李四",80,165,"计算机学院","上海")); students.add(new Student("王五",91,170,"财经学院","上海")); students.add(new Student("赵明",80,182,"计算机学院","北京")); students.add(new Student("钱海",75,181,"计算机学院","广州")); students.add(new Student("孙理",82,172,"财经学院","上海")); students.add(new Student("周伟",90,168,"电气学院","广州")); students.add(new Student("郑亮",80,178,"财经学院","广州")); System.out.println("原始数据:"); students.forEach(s ->{ System.out.println(s.getName()+" "+ s.getScore()+" "+s.getHeight()+" "+s.getCollege()+""+s.getAddress());}); System.out.println("按照分数降序排序,当分数相同时, 按照身高升序排序"); students.sort(comparing(Student::getScore).reversed().thenComparing(Student::getHeight)); students.forEach(s ->{ System.out.println(s.getName()+" "+ s.getScore()+" "+s.getHeight()+" "+s.getCollege()+""+s.getAddress());}); System.out.println("按照分数降序排序,当分数相同时, 按照身高降序排序"); Collections.sort(students, new Comparator () { public int compare(Student student1, Student student2) { if(student1.getScore().equals(student2.getScore())){ return student2.getHeight() - student1.getHeight(); }else{ return student2.getScore() - student1.getScore(); } } }); students.forEach(s ->{ System.out.println(s.getName()+" "+ s.getScore()+" "+s.getHeight()+" "+s.getCollege()+""+s.getAddress());}); }
2.3按照对象自定义单属性的顺序排序
/** * 对象自定义单属性的顺序排序 */ private void entitySort3(){ //Student 的 list 集合 Liststudents = new ArrayList<>(); students.add(new Student("张三",90,180,"电气学院","北京")); students.add(new Student("李四",80,165,"计算机学院","上海")); students.add(new Student("王五",91,170,"财经学院","上海")); students.add(new Student("赵明",80,182,"计算机学院","北京")); students.add(new Student("钱海",75,181,"计算机学院","广州")); students.add(new Student("孙理",82,172,"财经学院","上海")); students.add(new Student("周伟",90,168,"电气学院","广州")); students.add(new Student("郑亮",80,178,"财经学院","广州")); System.out.println("原始数据:"); students.forEach(s ->{ System.out.println(s.getName()+" "+ s.getScore()+" "+s.getHeight()+" "+s.getCollege()+""+s.getAddress());}); System.out.println("自定义按照地区(北京,上海,广州)排序:"); List addressOrder = Arrays.asList("北京","上海","广州"); Collections.sort(students, new Comparator () { public int compare(Student student1, Student student2) { int io1 = addressOrder.indexOf(student1.getAddress()); int io2 = addressOrder.indexOf(student2.getAddress()); return io1 - io2; } }); students.forEach( s ->{ System.out.println(s.getName()+" "+ s.getScore()+" "+s.getHeight()+" "+s.getCollege()+""+s.getAddress());}); }
2.4按照对象自定义多属性的顺序排序
/** * 对象自定义多属性的顺序排序 */ private void entitySort4(){ //Student 的 list 集合 Liststudents = new ArrayList<>(); students.add(new Student("张三",90,180,"电气学院","北京")); students.add(new Student("李四",80,165,"计算机学院","上海")); students.add(new Student("王五",91,170,"财经学院","上海")); students.add(new Student("赵明",80,182,"计算机学院","北京")); students.add(new Student("钱海",75,181,"计算机学院","广州")); students.add(new Student("孙理",82,172,"财经学院","上海")); students.add(new Student("周伟",90,168,"电气学院","广州")); students.add(new Student("郑亮",80,178,"财经学院","广州")); System.out.println("原始数据:"); students.forEach(s ->{ System.out.println(s.getName()+" "+ s.getScore()+" "+s.getHeight()+" "+s.getCollege()+""+s.getAddress());}); System.out.println("先按照学院(电气学院,计算机学院,财经学院)排序, 当学院相同时,按照地区(北京,上海,广州)排序"); List collegeOrder = Arrays.asList("电气学院","计算机学院","财经学院"); List Collections.sort(students, new ComparatoraddressOrder = Arrays.asList("北京","上海","广州"); () { public int compare(Student student1, Student student2) { if(student1.getCollege().equals(student2.getCollege())){ int io1 = addressOrder.indexOf(student1.getAddress()); int io2 = addressOrder.indexOf(student2.getAddress()); return io1 - io2; }else{ int io1 = collegeOrder.indexOf(student1.getCollege()); int io2 = collegeOrder.indexOf(student2.getCollege()); return io1 - io2; } } }); students.forEach( s ->{ System.out.println(s.getName()+" "+ s.getScore()+" "+s.getHeight()+" "+s.getCollege()+""+s.getAddress());}); }
2.5按照对象属性自定义混合排序
/** * 对象属性自定义混合排序 */ private void entitySort5(){ //Student 的 list 集合 Liststudents = new ArrayList<>(); students.add(new Student("张三",90,180,"电气学院","北京")); students.add(new Student("李四",80,165,"计算机学院","上海")); students.add(new Student("王五",91,170,"财经学院","上海")); students.add(new Student("赵明",80,182,"计算机学院","北京")); students.add(new Student("钱海",75,181,"计算机学院","广州")); students.add(new Student("孙理",82,172,"财经学院","上海")); students.add(new Student("周伟",90,168,"电气学院","广州")); students.add(new Student("郑亮",80,178,"财经学院","广州")); System.out.println("原始数据:"); students.forEach(s ->{ System.out.println(s.getName()+" "+ s.getScore()+" "+s.getHeight()+" "+s.getCollege()+""+s.getAddress());}); //先按照分数从高到低排序,当分数相同时,然后按照地区(北京,上海,广州)排序 List addressOrder = Arrays.asList("北京","上海","广州"); Collections.sort(students, new Comparator System.out.println(""); System.out.println("先按照分数从高到低排序,当分数相同时,然后按照地区(北京,上海,广州)排序:"); students.forEach( s ->{ System.out.println(s.getName()+" "+ s.getScore()+" "+s.getHeight()+" "+s.getCollege()+""+s.getAddress());}); }() { public int compare(Student student1, Student student2) { if(student1.getScore().equals(student2.getScore())){ int io1 = addressOrder.indexOf(student1.getAddress()); int io2 = addressOrder.indexOf(student2.getAddress()); return io1 - io2; }else{ return student2.getScore() - student1.getScore(); } } });