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: For int datatype
Collections.sort(agentDtoList, (o1, o2) -> o1.getCustomerCount() - o2.getCustomerCount());
or even:
Collections.sort(agentDtoList, Comparator.comparing(AgentSummaryDTO::getCustomerCount));
For String datatype (as in comment)
Collections.sort(list, (o1, o2) -> (o1.getAgentName().compareTo(o2.getAgentName())));
..it expects getter AgentSummaryDTO.getCustomerCount()
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: For int datatype
Collections.sort(agentDtoList, (o1, o2) -> o1.getCustomerCount() - o2.getCustomerCount());
or even:
Collections.sort(agentDtoList, Comparator.comparing(AgentSummaryDTO::getCustomerCount));
For String datatype (as in comment)
Collections.sort(list, (o1, o2) -> (o1.getAgentName().compareTo(o2.getAgentName())));
..it expects getter AgentSummaryDTO.getCustomerCount()
for anyone who looks for answer yet:
you can also sort your list with JAVA-8 Stream-API.
List<AgentSummaryDTO> sortedList = agentDtoList.stream()
.sorted(Comparator.comparing(AgentSummaryDTO::getCustomerCount).reversed())
.collect(Collectors.toList());
Videos
How do i alphabetically sort an arraylist of objects, based on the objects field "name". I have object Car, with a compareTo method:
@Override
public int compareTo(Car cName) {
int last = this.carName.compareTo(cName.carName);
return last == 0 ? this.carName.compareTo(cName.carName) : last;
}In a separate class there is an array list that captures all of these Car Objects. I need to sort that list alphabetically, then print out the table of Car Objects in alphabetical order. Im stuck trying to implement the above into the sort for the arraylist that i will iteratively print over.
I have looked at Collections.sort() but i don't want to edit the class name to implement comparator.
public void printRacers() {
// get
//
ArrayList<Car> carNames = new ArrayList<>();
carNames.addAll(racers);
System.out.println("Car name Race Car number");
for (int i = 0; i < carNames.size(); i++) {
Car c = carNames.get(i);
String cName = c.getCarName();
String cRace = d.getRaceName();
int sNum = c.getCarNumber();
String outputString = cName + " " + cClass + " " + sNum;
System.out.println(outputString);
}Im really stuck on this and would appreciate anyone's help!
Thanks
I'm trying to sort an ArrayList in alphabetical order, based on an attribute of the objects the ArrayList contains. For example: I have an object that represents a product and I want to sort the products based on the suppliers name of the attribute. This attribute is a string and could be something like "Old Navy", "Gap", "Peebles", etc.
Is it possible to do a lambda and sort based on product.supplierName? I was thinking of just putting everything into a HashMap with the key being the suppliers name, and then looping over the keys and appending each keys value to an ArrayList, but this doesn't seem like the most efficient method.