JDK1.8对List指定字段进行排序

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JDK1.8对List指定字段进行排序相关的知识,希望对你有一定的参考价值。

参考技术A import java.util.ArrayList;

import java.util.Collections;

import java.util.Comparator;

import java.util.List;

import com.wubei.demo.bean.Order;

/**

* List根据指定字段进行排序

*

*/

public class ListStreamTest

public static void main(String[] args)

String orderId1 = "2321837281372913";

String userId1 = "20180701001";

String orderId2 = "2321837281372914";

String userId2 = "20180701002";

String orderId3 = "2321837281372912";

String userId3 = "20180701003";

String orderId4 = "2321837281372918";

String userId4 = "20180701005";

String orderId5 = "2321837281372918";

String userId5 = "20180701004";

Order order = new Order();

order.setUserId(userId1);

order.setOrderId(orderId1);

Order order1 = new Order();

order1.setOrderId(orderId2);

order1.setUserId(userId2);

Order order2 = new Order();

order2.setOrderId(orderId3);

order2.setUserId(userId3);

Order order3 = new Order();

order3.setOrderId(orderId4);

order3.setUserId(userId4);

Order order4 = new Order();

order4.setOrderId(orderId5);

order4.setUserId(userId5);

List<Order> orderList = new ArrayList<Order>();

orderList.add(order);

orderList.add(order1);

orderList.add(order2);

orderList.add(order3);

orderList.add(order4);

//1.jdk8 lambda排序,带参数类型

orderList.sort(( Order ord1, Order ord2) -> ord2.getOrderId().compareTo(ord1.getOrderId()));

//2.jdk8 lambda排序,不带参数类型

orderList.sort(( ord1, ord2) -> ord2.getOrderId().compareTo(ord1.getOrderId()));

//3.jdk8 升序排序,Comparator提供的静态方法

Collections.sort(orderList, Comparator.comparing(Order::getOrderId));

//4.jdk8 降序排序,Comparator提供的静态方法

Collections.sort(orderList, Comparator.comparing(Order::getOrderId).reversed());

//5.jdk8 组合排序,Comparator提供的静态方法,先按orderId排序,orderId相同的按userId排序

Collections.sort(orderList, Comparator.comparing(Order::getOrderId).reversed().thenComparing(Order::getUserId));

orderList.stream().forEach(str -> System.out.println(str.getOrderId()+"/" + str.getUserId()));



JDK1.8对List指定字段进行排序

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

import com.wubei.demo.bean.Order;


/**
* List根据指定字段进行排序
*
*/
public class ListStreamTest {

public static void main(String[] args) {


String orderId1 = "2321837281372913";
String userId1 = "20180701001";
String orderId2 = "2321837281372914";
String userId2 = "20180701002";
String orderId3 = "2321837281372912";
String userId3 = "20180701003";
String orderId4 = "2321837281372918";
String userId4 = "20180701005";
String orderId5 = "2321837281372918";
String userId5 = "20180701004";

Order order = new Order();
order.setUserId(userId1);
order.setOrderId(orderId1);
Order order1 = new Order();
order1.setOrderId(orderId2);
order1.setUserId(userId2);

Order order2 = new Order();
order2.setOrderId(orderId3);
order2.setUserId(userId3);
Order order3 = new Order();
order3.setOrderId(orderId4);
order3.setUserId(userId4);
Order order4 = new Order();
order4.setOrderId(orderId5);
order4.setUserId(userId5);
List<Order> orderList = new ArrayList<Order>();
orderList.add(order);
orderList.add(order1);
orderList.add(order2);
orderList.add(order3);
orderList.add(order4);

//1.jdk8 lambda排序,带参数类型
orderList.sort(( Order ord1, Order ord2) -> ord2.getOrderId().compareTo(ord1.getOrderId()));

//2.jdk8 lambda排序,不带参数类型
orderList.sort(( ord1, ord2) -> ord2.getOrderId().compareTo(ord1.getOrderId()));

//3.jdk8 升序排序,Comparator提供的静态方法
Collections.sort(orderList, Comparator.comparing(Order::getOrderId));

//4.jdk8 降序排序,Comparator提供的静态方法
Collections.sort(orderList, Comparator.comparing(Order::getOrderId).reversed());

//5.jdk8 组合排序,Comparator提供的静态方法,先按orderId排序,orderId相同的按userId排序
Collections.sort(orderList, Comparator.comparing(Order::getOrderId).reversed().thenComparing(Order::getUserId));

orderList.stream().forEach(str -> System.out.println(str.getOrderId()+"/" + str.getUserId()));

}
}
































































以上是关于JDK1.8对List指定字段进行排序的主要内容,如果未能解决你的问题,请参考以下文章

jdk1.7和jdk1.8对list集合根据对象的属性进行排序

集合List根据多个字段进行排序

java list按照元素对象的指定多个字段属性进行排序

java list按照元素对象的指定多个字段属性进行排序

java 中 List<T>如何按照T中的一个字段排序?

jsonarray按指定字段进行排序