2012-09-22 7 views
12

Mam następujący zbiór:Sortuj zbiór obiekt Java w oparciu o jedno pole w nim

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

Gdzie AgentSummaryDTO wygląda następująco:

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

Teraz muszę uporządkować kolekcję agentDtoList oparciu o w polu customerCount, jak to osiągnąć?

+1

Jeśli używałeś tych tagów do wyszukiwania w javadoc, mógłbyś trafić w to: http://docs.oracle.com/javase/6/docs/api/java/util/Collections.html#sort(java.util. List, java.util.Comparator) – Vikdor

Odpowiedz

36

tutaj jest mój "1liner":

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

UPDATE Java 8: Dla int typ danych

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

Dla typ danych String (jak w komentarzu)

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

..powinie się spodziewać r AgentSummaryDTO.getCustomerCount()

+1

Użyłem return agentSummary1.getCustomerCount(). CompareTo (agentSummary2.getCustomerCount()); – 1355

+0

Co zrobić, jeśli muszę posortować ten sam przykład z nazwą agenta zamiast konta klienta? Muszę sortować na polu ciągu. proszę sugerować –

+4

@ TanGarg zamiast 'return o1.getCustomerCount() - o2.getCustomerCount();', miałbyś 'o1.getAgentName(). compareTo (o2.getAgentName())' –

3

Wystarczy popatrzeć na poniższym kodzie.

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

The answer by Jiri Kremser można uprościć jeszcze bardziej, co naprawdę jest pełna Java 8 sposobów, aby to zrobić:

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

To po prostu porównuje przez pole liczby całkowitej, i działa dobrze od Integer narzędzia Comparable.

Jeszcze roztworu czyszczącego może być użycie wbudowanego comparingInt() sposób:

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

Oczywiście, może to być wyrażone również krótszy statycznie importowania sort i comparingInt:

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

UPDATE dla języka Java 8 To działa

Collections.sort(agentDtoList, (o1, o2) -> o1.getCustomerCount() - o2.getCustomerCount()); 
Powiązane problemy