Java中List实现多字段排序功能

实体类TestObj包含a,b,c,三个字段,分别按照a字段升序,b字段降序,c字段升序来排序,

具体代码如下:

TestObj类:

package com.test;

public class TestObj {
	private String a;
	private String b;
	private String c;
	public TestObj(String a, String b, String c) {
		super();
		this.a = a;
		this.b = b;
		this.c = c;
	}
	public String getA() {
		return a;
	}
	public void setA(String a) {
		this.a = a;
	}
	public String getB() {
		return b;
	}
	public void setB(String b) {
		this.b = b;
	}
	public String getC() {
		return c;
	}
	public void setC(String c) {
		this.c = c;
	}
	@Override
	public String toString() {
		return "TestObj [a=" + a + ", b=" + b + ", c=" + c + "]";
	}
	
}

main方法:

package com.test;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

public class TestCompare {
	
	public static void main(String[] args) {
		TestObj obj1=new TestObj("0005", "0001", "0005");
		TestObj obj2=new TestObj("0005", "0001", "0004");
		TestObj obj3=new TestObj("0005", "0003", "0004");
		TestObj obj4=new TestObj("0004", "0004", "0006");
		TestObj obj5=new TestObj("0004", "0005", "0007");
		TestObj obj6=new TestObj("0001", "0006", "0009");
		TestObj obj7=new TestObj("0001", "0007", "0008");
		TestObj obj8=new TestObj("0005", "0003", "0003");
		
		List<TestObj> list=new ArrayList<TestObj>();
		list.add(obj1);
		list.add(obj2);
		list.add(obj3);
		list.add(obj4);
		list.add(obj5);
		list.add(obj6);
		list.add(obj7);
		list.add(obj8);
		
        //第一个字段排序
        System.out.println("第一个字段排序");
		list.sort(Comparator.comparing(TestObj::getA, Comparator.naturalOrder()));
		for(int idx=0;idx<list.size();idx++){
			System.out.println(list.get(idx).toString());
		}
		//前两个字段排序
		System.out.println("前两个字段排序");
		list.sort(Comparator.comparing(TestObj::getA, Comparator.naturalOrder()).thenComparing(TestObj::getB, Comparator.reverseOrder()));
		for(int idx=0;idx<list.size();idx++){
			System.out.println(list.get(idx).toString());
		}
		//三个字段排序
		System.out.println("三个字段排序");
		list.sort(Comparator.comparing(TestObj::getA, Comparator.naturalOrder()).thenComparing(TestObj::getB, Comparator.reverseOrder()).thenComparing(TestObj::getC, Comparator.naturalOrder()));
		for(int idx=0;idx<list.size();idx++){
			System.out.println(list.get(idx).toString());
		}

//		for(int idx=0;idx<list.size();idx++){
//			System.out.println(list.get(idx).toString());
//		}
//		System.out.println("排序");
//		List<TestObj> listNew=list.stream().sorted(Comparator.comparing(TestObj::getA, Comparator.naturalOrder()).thenComparing(TestObj::getB, Comparator.reverseOrder()).thenComparing(TestObj::getC, Comparator.naturalOrder())).collect(Collectors.toList());
//		for(int idx=0;idx<listNew.size();idx++){
//			System.out.println(listNew.get(idx).toString());
//		}
	}

}

运行结果:

第一个字段排序
TestObj [a=0001, b=0006, c=0009]
TestObj [a=0001, b=0007, c=0008]
TestObj [a=0004, b=0004, c=0006]
TestObj [a=0004, b=0005, c=0007]
TestObj [a=0005, b=0001, c=0005]
TestObj [a=0005, b=0001, c=0004]
TestObj [a=0005, b=0003, c=0004]
TestObj [a=0005, b=0003, c=0003]
前两个字段排序
TestObj [a=0001, b=0007, c=0008]
TestObj [a=0001, b=0006, c=0009]
TestObj [a=0004, b=0005, c=0007]
TestObj [a=0004, b=0004, c=0006]
TestObj [a=0005, b=0003, c=0004]
TestObj [a=0005, b=0003, c=0003]
TestObj [a=0005, b=0001, c=0005]
TestObj [a=0005, b=0001, c=0004]
三个字段排序
TestObj [a=0001, b=0007, c=0008]
TestObj [a=0001, b=0006, c=0009]
TestObj [a=0004, b=0005, c=0007]
TestObj [a=0004, b=0004, c=0006]
TestObj [a=0005, b=0003, c=0003]
TestObj [a=0005, b=0003, c=0004]
TestObj [a=0005, b=0001, c=0004]
TestObj [a=0005, b=0001, c=0005]

list.stream().sorted.....collect(Collectors.toList()):返回新的有序集合,原集合顺序不变

list.sort...:返回void,原集合顺序变为有序

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值