public class Table_3 {
private String id;
private String name;
private String nameId;
private String nametype;
。。。。。
}
public class testSort {
public static void main(String[] args) {
Table_3 table1 = new Table_3();
table1.setId("1");
table1.setName("a");
table1.setNameId("因为");
table1.setNametype("!");
Table_3 table2 = new Table_3();
table2.setId("3");
table2.setName("c");
table2.setNameId("世界");
table2.setNametype("+a");
Table_3 table3 = new Table_3();
table3.setId("2");
table3.setName("c");
table3.setNameId("精彩c");
table3.setNametype("-");
Table_3 table4 = new Table_3();
table4.setId("3");
table4.setName("b");
table4.setNameId("jingcaic");
table4.setNametype("+b");
Table_3 table5 = new Table_3();
table5.setId("3");
table5.setName("a");
table5.setNameId("精彩a");
table5.setNametype("&");
List<Table_3> list = new ArrayList<>();
list.add(table1);
list.add(table2);
list.add(table3);
list.add(table4);
list.add(table5);
System.out.println("--------原始顺序---------");
for(Table_3 h : list){
System.out.println(h.getId()+"--"+h.getName());
}
list.sort((Table_3 a, Table_3 b) -> a.getId().compareTo(b.getId()));
System.out.println("----Lambda----Id升序---------");
for(Table_3 h : list){
System.out.println(h.getId()+"--"+h.getName());
}
//sql的中文字段是 nvarchar,对应的java把字符串也转成unicode 来排序
list.sort((Table_3 a, Table_3 b) -> Collator.getInstance(Locale.CHINA).compare(a.getNameId(), b.getNameId() ) );
System.out.println("--------中文升序---------");
for(Table_3 h : list){
System.out.println(h.getId()+"--"+h.getName()+"--"+h.getNameId());
}
//个人认为按照String的一般排序比较就行,有意见可以提出来
list.sort((Table_3 a, Table_3 b) -> a.getNametype().compareTo(b.getNametype()));
System.out.println("--------特殊字符升序---------");
for(Table_3 h : list){
System.out.println(h.getId()+"--"+h.getNametype());
}
Collections.sort(list, Comparator.comparing(Table_3 :: getId).thenComparing(Table_3 :: getName).reversed());
System.out.println("--------Id降序Name降序---------");
list.forEach(h -> System.out.println(h.getId()+"--"+h.getName()));
Collections.sort(list, Comparator.comparing(Table_3 :: getId).reversed().thenComparing(Table_3 :: getName));
System.out.println("--------Id降序Name升序---------");
list.forEach(h -> System.out.println(h.getId()+"--"+h.getName()));
list.sort((Table_3 a, Table_3 b) -> b.getId().compareTo(a.getId()));
System.out.println("--------Id降序---------");
list.forEach(h -> System.out.println(h.getId()+"--"+h.getName()));
Collections.sort(list, Comparator.comparing(Table_3 :: getName).reversed());
System.out.println("--------Name降序---------");
list.forEach(h -> System.out.println(h.getId()+"--"+h.getName()));
Collections.sort(list, Comparator.comparing(Table_3 :: getId).thenComparing(Table_3 :: getName));
System.out.println("--------Id升序Name升序---------");
list.forEach(h -> System.out.println(h.getId()+"--"+h.getName()));
/*这个排序倒是有疑义,
比如 reversed() 是n ,
1. a b n =A B
2. a n b n = A B
3. a n b = A b
开始以为 ( a n b ) n = a B,其实跟 2 没差别
4. a n n b n 的话, 是对 a n n --> a , 然后 a b n = A B
所以就得出
a n n n b n --> a n n n = A ---> A b n = a B
*/
Collections.sort(list, Comparator.comparing(Table_3 :: getId).reversed().reversed().reversed().thenComparing(Table_3 :: getName).reversed());
System.out.println("--------Id升序Name降序---------");
list.forEach(h -> System.out.println(h.getId()+"--"+h.getName()+"--"+h.getNameId()));
List<Table_3> list2 = list.stream().filter(n -> n.getName().equals("c")).sorted((Table_3 a, Table_3 b)-> b.getId().compareTo(a.getId())).collect(Collectors.toList());
System.out.println("--------筛选名字=c的Id降序---------");
list2.forEach(h -> System.out.println(h.getId()+"--"+h.getName()));
System.out.println("--------name自定义排序 cab---------");
list.sort((Table_3 a, Table_3 b) -> ownOrder(a.getName()).compareTo(ownOrder(b.getName()) ));
list.forEach(h -> System.out.println(h.getId()+"--"+h.getName()));
}
private static String ownOrder(String order){
if(order.equals("a")){
return "1";
}else if(order.equals("b")){
return "2";
}else if(order.equals("c")){
return "0";
}else{
return order;
}
}
}