Java - List排序 需求:用户有成绩和年龄。按成绩降序排序,若成绩相同,则按年龄正序排序。需求:用户有成绩和年龄。按成绩排序,若成绩相同,则按年龄排序。

List排序方法

主要有三种方法(按推荐度排序):

  1. JDK8的stream
  2. Comparator#compare()
  3. Comparable#compareTo()

法1:list的sort()

package com.example.a;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
class User{
 private Integer score;
 private Integer age;
 public User(Integer score, Integer age){
 super();
 this.score = score;
 this.age = age;
 }
 public Integer getScore() {
 return score;
 }
 public void setScore(Integer score) {
 this.score = score;
 }
 public Integer getAge() {
 return age;
 }
 public void setAge(Integer age) {
 this.age = age;
 }
}
public class Demo {
 public static void main(String[] args) {
 List users = new ArrayList();
 users.add(new User(95, 26));
 users.add(new User(84, 23));
 users.add(new User(96, 25));
 users.add(new User(95, 24));
 // 单字段排序
 users.sort(Comparator.comparing(User::getAge));
 for(User user : users){
 System.out.println(user.getScore() + "," + user.getAge());
 }
 System.out.println("---------------------------------");
 // 多字段排序(法1)
 users.sort((o1, o2) -> {
 // 这里必须要在中间加分隔符。否则,若都是数字,会变成数字相加,再转为字符串
 String tmp1 = o1.getScore() + ":" + o1.getAge();
 String tmp2 = o2.getScore() + ":" + o2.getAge();
 return tmp1.compareTo(tmp2);
 });
 for(User user : users){
 System.out.println(user.getScore() + ":" + user.getAge());
 }
 System.out.println("---------------------------------");
 // 多字段排序(法2)
 users.sort((o1, o2) -> {
 int i = o2.getScore() - o1.getScore();
 if (i == 0) {
 return o1.getAge() - o2.getAge();
 }
 return i;
 });
 for(User user : users){
 System.out.println(user.getScore() + "," + user.getAge());
 }
 }
}
//测试输出
84,23
95,24
96,25
95,26
---------------------------------
84:23
95:24
95:26
96:25
---------------------------------
96,25
95,24
95,26
84,23

法2:JDK8的stream

方法说明
sorted()自然排序(从小到大),流中元素需实现Comparable接口。 例:list.stream().sorted()
sorted(Comparator com)定制排序。常用以下几种:
list.stream().sorted(Comparator.reverseOrder()) //倒序排序(从大到小)
list.stream().sorted(Comparator.comparing(Student::getAge)) //顺序排序(从小到大)
list.stream().sorted(Comparator.comparing(Student::getAge).reversed()) // 倒序排序(从大到小)

返回排序后的流

//4、sorted:排序,根据名字倒序
userList.stream().sorted(Comparator.comparing(User::getName).reversed()).collect(Collectors.toList()).forEach(System.out::println);

原始类型排序

List list = Arrays.asList("aa", "ff", "dd");
//String 类自身已实现Comparable接口
list.stream().sorted().forEach(System.out::println);
 
//结果:
aa
dd
ff

对象单字段排序

User u1 = new User("dd", 40);
User u2 = new User("bb", 20);
User u3 = new User("aa", 20);
User u4 = new User("aa", 30);
List userList = Arrays.asList(u1, u2, u3, u4);
 
//按年龄升序
userList.stream().sorted(Comparator.comparing(User::getAge))
        .forEach(System.out::println);
 
//结果
User(name=bb, age=20)
User(name=aa, age=20)
User(name=aa, age=30)
User(name=dd, age=40)

对象多字段、全部升序排序 

//先按年龄升序,年龄相同则按姓名升序
User u1 = new User("dd", 40);
User u2 = new User("bb", 20);
User u3 = new User("aa", 20);
User u4 = new User("aa", 30);
List userList = Arrays.asList(u1, u2, u3, u4);
 
// 写法1(推荐)
userList.stream().sorted(Comparator
                .comparing(User::getAge)
                .thenComparing(User::getName)
        // 可以写多个.thenComparing
).forEach(System.out::println);
 
System.out.println("------------------------------------");
 
// 写法2
userList.stream().sorted(
        (o1, o2) -> {
            String tmp1 = o1.getAge() + o1.getName();
            String tmp2 = o2.getAge() + o2.getName();
            return tmp1.compareTo(tmp2);
        }
).forEach(System.out::println);
 
System.out.println("------------------------------------");
 
// 写法3
userList.stream().sorted(
        (o1, o2) -> {
            if (!o1.getAge().equals(o2.getAge())) {
                return o1.getAge().compareTo(o2.getAge());
            } else {
                return o1.getName().compareTo(o2.getName());
            }
        }
).forEach(System.out::println);
 
//结果
User(name=aa, age=20)
User(name=bb, age=20)
User(name=aa, age=30)
User(name=dd, age=40)
------------------------------------
User(name=aa, age=20)
User(name=bb, age=20)
User(name=aa, age=30)
User(name=dd, age=40)
------------------------------------
User(name=aa, age=20)
User(name=bb, age=20)
User(name=aa, age=30)
User(name=dd, age=40)

对象多字段、升序+降序

//先按年龄升序,年龄相同则按姓名降序
User u1 = new User("dd", 40);
User u2 = new User("bb", 20);
User u3 = new User("aa", 20);
User u4 = new User("aa", 30);
List userList = Arrays.asList(u1, u2, u3, u4);
 
userList.stream().sorted(
        (o1, o2) -> {
            if (!o1.getAge().equals(o2.getAge())) {
                return o1.getAge().compareTo(o2.getAge());
            } else {
                return o2.getName().compareTo(o1.getName());
            }
        }
).forEach(System.out::println);
 
//结果
User(name=bb, age=20)
User(name=aa, age=20)
User(name=aa, age=30)
User(name=dd, age=40)

 

法3:Comparator#compare()

需求:用户有成绩和年龄。按成绩排序,若成绩相同,则按年龄排序。

package org.example.a;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
class User{
 private int score;
 private int age;
 public User(int score, int age){
 super();
 this.score = score;
 this.age = age;
 }
 public int getScore() {
 return score;
 }
 public void setScore(int score) {
 this.score = score;
 }
 public int getAge() {
 return age;
 }
 public void setAge(int age) {
 this.age = age;
 }
}
public class Demo {
 public static void main(String[] args) {
 List users = new ArrayList();
 users.add(new User(95, 26));
 users.add(new User(84, 23));
 users.add(new User(96, 25));
 users.add(new User(95, 24));
 Collections.sort(users, new Comparator() {
 @Override
 public int compare(User o1, User o2) {
 int i = o2.getScore() - o1.getScore();
 if(i == 0){
 return o1.getAge() - o2.getAge();
 }
 return i;
 }
 });
 for(User user : users){
 System.out.println(user.getScore() + "," + user.getAge());
 }
 }
}
//执行结果
96,25
95,24
95,26
84,23

法4:Comparable#compareTo()

默认按增序排序:

需求:用户有成绩和年龄。按成绩降序排序,若成绩相同,则按年龄正序排序。

package org.example.a;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
class User implements Comparable{
 private int score;
 private int age;
 public User(int score, int age){
 super();
 this.score = score;
 this.age = age;
 }
 public int getScore() {
 return score;
 }
 public void setScore(int score) {
 this.score = score;
 }
 public int getAge() {
 return age;
 }
 public void setAge(int age) {
 this.age = age;
 }
 @Override
 public int compareTo(User o) {
 int i = o.getScore() - this.getScore();
 if(i == 0){
 return this.getAge() - o.getAge();
 }
 return i;
 }
}
public class Demo {
 public static void main(String[] args) {
 List users = new ArrayList();
 users.add(new User(95, 26));
 users.add(new User(84, 23));
 users.add(new User(96, 25));
 users.add(new User(95, 24));
 Collections.sort(users);
 for(User user : users){
 System.out.println(user.getScore() + "," + user.getAge());
 }
 }
}
//执行结果
96,25
95,24
95,26
84,23

作者:MinggeQingchun原文地址:https://blog.csdn.net/MinggeQingchun/article/details/136463954

%s 个评论

要回复文章请先登录注册