2012-09-22 7 views
12

Ho la seguente collezione:Ordinare un oggetto di collezione Java basata su un campo in essa

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

Dove AgentSummaryDTO assomiglia a questo:

public class AgentSummaryDTO implements Serializable { 
    private Long id; 
    private String agentName; 
    private String agentCode; 
    private String status; 
    private Date createdDate; 
    private Integer customerCount; 
} 

Ora devo ordinare la raccolta agentDtoList sulla base di il campo customerCount, come ottenere questo?

+1

Se avessi usato quei tag per cercare in javadoc, avresti potuto premere questo: http://docs.oracle.com/javase/6/docs/api/java/util/Collections.html#sort(java.util. List, java.util.Comparator) – Vikdor

risposta

36

qui è il mio "1liner":

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

aggiornamento per Java 8: Per int tipo di dati

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

per la stringa tipo di dati (come nel commento)

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

.. si aspetta gette r AgentSummaryDTO.getCustomerCount()

+1

Ho usato l'agent di ritornoSummary1.getCustomerCount(). CompareTo (agentSummary2.getCustomerCount()); – 1355

+0

Cosa succede se ho bisogno di ordinare lo stesso esempio con agentname invece di customercount? Ho bisogno di ordinare su un campo di stringa. per favore suggerire –

+4

@TanuGarg invece di 'return o1.getCustomerCount() - o2.getCustomerCount();', si dovrebbe 'o1.getAgentName(). compareTo (o2.getAgentName())' –

3

Date un'occhiata al codice qui sotto.

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

Il answer by Jiri Kremser può essere semplificata ulteriormente, che è davvero il modo in cui Java 8 pieno per farlo:

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

Questo confronto semplicemente il campo intero, e funziona bene dal Integer attrezzi Comparable.

Una soluzione altrettanto potrebbe essere quella di utilizzare il built-in comparingInt() metodo:

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

Naturalmente, questo potrebbe essere espresso ancora più breve importando staticamente sort e comparingInt:

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

UPDATE per Java 8 È lavoro

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