sorting - Sort a Java collection object based on one field in it -
i have following collection:
collection<agentsummarydto> agentdtolist = new arraylist<agentsummarydto>(); where agentsummarydto looks this:
public class agentsummarydto implements serializable { private long id; private string agentname; private string agentcode; private string status; private date createddate; private integer customercount; } now have sort collection agentdtolist based on customercount field, how achieve this?
here "1liner":
collections.sort(agentdtolist, new comparator<agentsummarydto>(){ public int compare(agentsummarydto o1, agentsummarydto o2){ return o1.getcustomercount() - o2.getcustomercount(); } }); update java 8:
collections.sort(agentdtolist, (o1, o2) -> o1.getcustomercount() - o2.getcustomercount()); ..it expects getter agentsummarydto.getcustomercount()
Comments
Post a Comment