2012-09-22 91 views
12

我有以下的集合:排序Java集合對象基於一個領域它

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; 
} 

現在我要進行排序基於集合agentDtoListcustomerCount字段,該如何實現?

+1

如果您已使用這些標記在javadoc中進行搜索,則可以點擊此處:http://docs.oracle.com/javase/6/docs/api/java/util/Collections.html#sort(java.util。 List,java.util.Comparator) – Vikdor

回答

36

這裏是我的 「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(list, (o1, o2) -> (o1.getAgentName().compareTo(o2.getAgentName()))); 

..他預計gette [R AgentSummaryDTO.getCustomerCount()

+1

我使用了return agentSummary1.getCustomerCount()。compareTo(agentSummary2.getCustomerCount()); – 1355

+0

如果我需要使用agentname而不是customercount對相同的示例進行排序,該怎麼辦?我需要在字符串字段上排序。請提供 –

+4

@TanuGarg,而不是'return o1.getCustomerCount() - o2.getCustomerCount();',您將擁有'o1.getAgentName()。compareTo(o2.getAgentName())' –

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) { 
     // TODO Auto-generated method stub 

     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

answer by Jiri Kremser甚至可以進一步簡化,這真的是做了完整的Java 8路:

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

這只是由整型字段進行比較,並且效果很好,因爲Integer工具Comparable

一個更清潔的解決方案可能是使用內置的comparingInt()方法:

sort(agentDtoList, comparingInt(AgentSummaryDTO::getCustomerCount)); 
0

UPDATE:

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

當然,這可以通過靜態導入sortcomparingInt表示更短對於Java 8它的工作

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