实体类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,原集合顺序变为有序