基于Java集合对象中的一个字段对其进行排序 [英] Sort a Java collection object based on one field in it
本文介绍了基于Java集合对象中的一个字段对其进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下集合:
Collection<AgentSummaryDTO> agentDtoList = new ArrayList<AgentSummaryDTO>();
其中 AgentSummaryDTO
如下所示: p>
Where AgentSummaryDTO
looks like this:
public class AgentSummaryDTO implements Serializable {
private Long id;
private String agentName;
private String agentCode;
private String status;
private Date createdDate;
private Integer customerCount;
}
现在我必须对 agentDtoList
基于
customerCount
字段,如何实现这一点?
Now I have to sort the collection agentDtoList
based on the customerCount
field, how to achieve this?
推荐答案
这里是我的1liner:
here is my "1liner":
Collections.sort(agentDtoList, new Comparator<AgentSummaryDTO>(){
public int compare(AgentSummaryDTO o1, AgentSummaryDTO o2){
return o1.getCustomerCount() - o2.getCustomerCount();
}
});
Java 8的更新:
UPDATE for Java 8:
Collections.sort(agentDtoList, (o1, o2) ->
o1.getCustomerCount() - o2.getCustomerCount());
..它期望getter AgentSummaryDTO.getCustomerCount()
..it expects getter AgentSummaryDTO.getCustomerCount()
这篇关于基于Java集合对象中的一个字段对其进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文