对LIST对象多个字段进行排序

本文介绍如何使用 Java 8 的 Stream API 对包含学生姓名、年龄及生日的列表进行复合排序。示例代码展示了如何实现按年龄升序、姓名不区分大小写的降序及生日降序的排序规则。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

jdk1.8之前的做法

参考: http://blog.youkuaiyun.com/enable1234___/article/details/53224740

jdk1.8新特性的做法:

参考: http://blog.youkuaiyun.com/aitangyong/article/details/54880228

Student.java

public class Student {
	private Long id;
	private String name;
	private String age;
	private Date birthday;

	public Student() {
	}

	public Student(Long id, String name, String age) {
		this.id = id;
		this.name = name;
		this.age = age;
	}

	public Student(String name, String age) {
		this.name = name;
		this.age = age;
	}

	public Student(String name, String age, Date birthday) {
		this.name = name;
		this.age = age;
		this.birthday = birthday;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getAge() {
		return age;
	}

	public void setAge(String age) {
		this.age = age;
	}

	public Date getBirthday() {
		return birthday;
	}

	public String getBirthdayStr() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		return sdf.format(birthday);
	}

	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}

	@Override
	public String toString() {
		return "Student [name=" + name + ", age=" + age + ", birthday=" + getBirthdayStr() + "]";
	}
}

Listtest.java

public class ListTest {
	@Test
	public void test() {
		List<Student> stuList = new ArrayList<Student>();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
		stuList.add(new Student("wang", "18", sdf.parse("2007/04/01")));
		stuList.add(new Student("li", "19", sdf.parse("2007/05/01")));
		stuList.add(new Student("liu", "20", sdf.parse("2006/04/01")));
		stuList.add(new Student("xi", "15", sdf.parse("2007/04/01")));
		stuList.add(new Student("li", "17", sdf.parse("2007/05/01")));
		stuList.add(new Student("wang", "19", sdf.parse("2007/06/01")));

		// age升序
		Comparator<Student> byIdASC = Comparator.comparing(Student::getAge);

		// named不分区大小写降序
		Comparator<Student> byNameDESC = Comparator.comparing(Student::getName, String.CASE_INSENSITIVE_ORDER)
				.reversed();

		// birthday 降序
		Comparator<Student> byBirthdayDESC = Comparator.comparing(Student::getBirthday).reversed();

		// 联合排序
		Comparator<Student> finalComparator = byIdASC.thenComparing(byNameDESC).thenComparing(byBirthdayDESC);

		List<Student> result = stuList.stream().sorted(finalComparator).collect(Collectors.toList());
		print(result);
		// 输出结果:
		// Student [name=xi, age=15, birthday=2007-04-01]
		// Student [name=li, age=17, birthday=2007-05-01]
		// Student [name=wang, age=18, birthday=2007-04-01]
		// Student [name=wang, age=19, birthday=2007-06-01]
		// Student [name=li, age=19, birthday=2007-05-01]
		// Student [name=liu, age=20, birthday=2006-04-01]

	}
}


Java中,对实体类List按照多个字段进行排序可以通过多种方式实现,其中一种常用的方法是使用`Comparator`接口。以下是一个详细的步骤和示例代码,帮助你理解如何实现这一功能。 假设我们有一个实体类`Person`,包含`name`、`age`和`height`字段: ```java public class Person { private String name; private int age; private double height; // 构造方法 public Person(String name, int age, double height) { this.name = name; this.age = age; this.height = height; } // Getter方法 public String getName() { return name; } public int getAge() { return age; } public double getHeight() { return height; } // toString方法 @Override public String toString() { return "Person{name='" + name + '\'' + ", age=" + age + ", height=" + height + '}'; } } ``` 接下来,我们需要对`Person`对象List进行排序排序规则为:首先按`age`升序排序,如果`age`相同,则按`height`降序排序。 ```java import java.util.ArrayList; import java.util.Comparator; import java.util.List; public class Main { public static void main(String[] args) { List<Person> people = new ArrayList<>(); people.add(new Person("Alice", 30, 1.65)); people.add(new Person("Bob", 25, 1.80)); people.add(new Person("Charlie", 30, 1.75)); // 按多个字段排序 people.sort(Comparator .comparingInt(Person::getAge) .thenComparing(Comparator.comparingDouble(Person::getHeight).reversed())); // 输出排序结果 for (Person person : people) { System.out.println(person); } } } ``` 在这个示例中,我们使用了`Comparator`接口的静态方法`comparingInt`和`thenComparing`来定义排序规则。首先按`age`升序排序,如果`age`相同,则按`height`降序排序(通过`reversed()`方法实现)。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值