Sort a java collection object based on one field in it

How will sort a collection in java.

I have a collection

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

where

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

Now I have to sort the collection agentDtoList based on the customerCount field

Answers


here is my 1liner:

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

UPDATE for Java 8:

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

..it expects getter AgentSummaryDTO.getCustomerCount()


Have a look at the Comparator and Collections classes.

A simple way would be to implement the Comparable interface in AgentSummaryDTO and then pass the list to Collections.sort().

If you can't edit AgentSummaryDTO, you need a Comparator as shown here: How to sort a List<Object> alphabetically using Object name field


Have a look at the code below.

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

}

Need Your Help

Multiply long or BigInteger by double without loss of precision

java double precision long-integer biginteger

I want to multiply a high precision integer (long or BigInteger) by a small double (think about something > 0 and &lt; 1) and get the arithmetically rounded integer (long or BigInteger) of the exact

SO_REUSEADDR with UDP sockets on Linux. Is it necessary?

linux udp time-wait setsockopt

My UDP socket is bind()ing to port 53 (DNS). Does UDP have a TIME_WAIT state or is using SO_REUSEADDR pointless on UDP sockets?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.