根据Java集合对象中的一个字段对其进行排序

时间:2012-09-22 08:34:02

标签: java sorting collections

我有以下系列:

Collection<AgentSummaryDTO> agentDtoList = new ArrayList<AgentSummaryDTO>();

AgentSummaryDTO看起来像这样:

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进行排序,如何实现这一目标?

5 个答案:

答案 0 :(得分:45)

这是我的“1liner”:

Collections.sort(agentDtoList, new Comparator<AgentSummaryDTO>(){
   public int compare(AgentSummaryDTO o1, AgentSummaryDTO o2){
      return o1.getCustomerCount() - o2.getCustomerCount();
   }
});

Java 8的更新: 对于int数据类型

 Collections.sort(agentDtoList, (o1, o2) -> o1.getCustomerCount() - o2.getCustomerCount());

甚至:

 Collections.sort(agentDtoList, Comparator.comparing(AgentSummaryDTO::getCustomerCount));

对于String数据类型(如注释中)

Collections.sort(list, (o1, o2) -> (o1.getAgentName().compareTo(o2.getAgentName())));

..它期待getter AgentSummaryDTO.getCustomerCount()

答案 1 :(得分:7)

查看ComparatorCollections类。

一种简单的方法是在AgentSummaryDTO中实施Comparable界面,然后将列表传递给Collections.sort()

如果您无法修改AgentSummaryDTO,则需要一个比较器,如下所示:How to sort a List<Object> alphabetically using Object name field

答案 2 :(得分:3)

看看下面的代码。

package test;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

public class AgentSummary {
    private Long id;
    private String agentName;
    private String agentCode;
    private String status;
    private Date createdDate;
    private Integer customerCount;

    /**
     * @param args
     */
    public static void main(String[] args) {
        new AgentSummary().addObjects();   
    }

    public void addObjects(){
        List<AgentSummaryDTO> agentSummary = new ArrayList<AgentSummaryDTO>();
        for (int j = 0; j < 10; j++) {
            agentSummary.add(new AgentSummaryDTO(j));
        }
        Collections.sort(agentSummary);

        for (AgentSummaryDTO obj : agentSummary) {
            System.out.println("File " + obj.getCustomerCount());
        }
    }
}

class AgentSummaryDTO implements Serializable, Comparable<AgentSummaryDTO> {

    private Long id;
    private String agentName;
    private String agentCode;
    private String status;
    private Date createdDate;
    private Integer customerCount;

    AgentSummaryDTO() {
        customerCount = null;
    }

    AgentSummaryDTO(int customerCount) {
        this.customerCount = customerCount;
    }

    /**
     * @return the id
     */
    public Long getId() {
        return id;
    }

    /**
     * @param id
     *            the id to set
     */
    public void setId(Long id) {
        this.id = id;
    }

    /**
     * @return the agentName
     */
    public String getAgentName() {
        return agentName;
    }

    /**
     * @param agentName
     *            the agentName to set
     */
    public void setAgentName(String agentName) {
        this.agentName = agentName;
    }

    /**
     * @return the agentCode
     */
    public String getAgentCode() {
        return agentCode;
    }

    /**
     * @param agentCode
     *            the agentCode to set
     */
    public void setAgentCode(String agentCode) {
        this.agentCode = agentCode;
    }

    /**
     * @return the status
     */
    public String getStatus() {
        return status;
    }

    /**
     * @param status
     *            the status to set
     */
    public void setStatus(String status) {
        this.status = status;
    }

    /**
     * @return the createdDate
     */
    public Date getCreatedDate() {
        return createdDate;
    }

    /**
     * @param createdDate
     *            the createdDate to set
     */
    public void setCreatedDate(Date createdDate) {
        this.createdDate = createdDate;
    }

    /**
     * @return the customerCount
     */
    public Integer getCustomerCount() {
        return customerCount;
    }

    /**
     * @param customerCount
     *            the customerCount to set
     */
    public void setCustomerCount(Integer customerCount) {
        this.customerCount = customerCount;
    }

    @Override
    public int compareTo(AgentSummaryDTO arg0) {

        if (this.customerCount > arg0.customerCount)
            return 0;
        else
            return 1;
    }
}

答案 3 :(得分:2)

Java 8更新它的工作

{{1}}

答案 4 :(得分:0)

对于任何正在寻找答案的人:

您还可以使用 JAVA-8 Stream-API 对列表进行排序。

List<AgentSummaryDTO> sortedList = agentDtoList.stream()
  .sorted(Comparator.comparing(AgentSummaryDTO::getCustomerCount).reversed())
  .collect(Collectors.toList());