29

我有以下收藏:

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字段对集合进行排序,如何实现呢?

4

7 回答 7

74

这是我的“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())));

..它需要吸气剂AgentSummaryDTO.getCustomerCount()

于 2012-09-22T09:55:09.860 回答
10

Jiri Kremser的答案可以进一步简化,这确实是 Java 8 的完整方法:

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

这只是通过整数字段进行比较,并且自Integer实现以来运行良好Comparable

一个更清洁的解决方案可能是使用内置comparingInt()方法:

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

当然,这可以通过静态导入sort和更短地表达comparingInt

sort(agentDtoList, comparingInt(AgentSummaryDTO::getCustomerCount));
于 2016-12-05T09:38:49.727 回答
7

看看ComparatorCollections类。

一种简单的方法是实现Comparable接口AgentSummaryDTO,然后将列表传递给Collections.sort().

如果您无法编辑AgentSummaryDTO,则需要一个 Comparator,如下所示:如何使用对象名称字段按字母顺序对 List<Object> 进行排序

于 2012-09-22T08:39:22.467 回答
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;
    }
}
于 2012-09-22T09:33:52.367 回答
3

Java 8的更新。它的工作原理:

Collections.sort(agentDtoList, (o1, o2) -> o1.getCustomerCount() - o2.getCustomerCount());
于 2017-02-16T06:02:28.957 回答
0

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

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

List<AgentSummaryDTO> sortedList = agentDtoList.stream()
  .sorted(Comparator.comparing(AgentSummaryDTO::getCustomerCount).reversed())
  .collect(Collectors.toList());
于 2021-06-12T10:37:10.313 回答
0

你可以使用这个代码

agentDtoList.sort((t1, t2) -> t1.getCustomerCount());
于 2021-11-15T18:02:51.260 回答