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()

Answer from Jiri Kremser on Stack Overflow
🌐
Coderanch
coderanch.com › t › 382108 › java › Sorting-List-objects-field
Sorting a List of objects by field (Java in General forum at Coderanch)
February 23, 2007 - Then call either java.util.Arrays.sort() (if you have an array) or java.util.Collections.sort() (if you have an actual List) to sort the items, passing an instance of your Comparator class as an argument. ... You can try using org.apache.commons.beanutils.BeanComparator.
🌐
BeginnersBook
beginnersbook.com › 2013 › 12 › java-arraylist-of-object-sort-example-comparable-and-comparator
Java ArrayList of Object Sort Example (Comparable And Comparator)
By Using Comparable interface .. You can sort on any field like name or tag etc… other than integer. Just change the implementation of compareTo method like · @Override public int compareTo(Student st) { Student student = st; return studentname.compareTo(student.getStudentName()); } ... ...
🌐
freeCodeCamp
freecodecamp.org › news › how-to-sort-a-list-in-java
How to Sort a List in Java – Java List Sorting Example
January 24, 2023 - It's best to use the Collections.sort() method when you have a collection of objects that need to be sorted based on one or more fields. For example, if you have a collection of Employee objects and you want to sort them by their last name, ...
🌐
Amir Boroumand
steelcityamir.com › blog › sort-list-of-objects-by-field-java
Sort a List of Objects by Field in Java · Amir Boroumand | Software engineer based in Pittsburgh, PA
May 9, 2018 - In this situation, we can use the sorted() method from the Stream interface introduced in Java 8. List<User> sortedUsers = users.stream() .sorted(Comparator.comparing(User::getCreatedOn)) .collect(Collectors.toList()); List<User> sortedUsers = users.stream() .sorted(Comparator.comparing(Us...
🌐
GitHub
gist.github.com › SheldonWangRJT › cdd2c1d0bfe092dea30a3e929aba325f
How to sort a List<Object> in JAVA in the best way?
Comparator is the most classic one but also the one with the longest typing, basically we need to create a new Comparator for our sorting.
🌐
BezKoder
bezkoder.com › home › java – sort arraylist of objects
Java - Sort ArrayList of Objects - BezKoder
December 27, 2019 - You will know how to: sort ArrayList of Objects by one field or multiple fields use custom Comparator to sort ArrayList of Objects implement Comparable interface to sort ArrayList of Objects conveniently Java ArrayList […]
Find elsewhere
🌐
DZone
dzone.com › data engineering › data › how to sort objects in java
How To Sort Objects In Java
February 27, 2018 - If we observe the output, we can see that "Sorted students list:" has students' information in reversed order (by name). So far students sorting was done based on the Student's "name" not by "no". If we want to sort by "no", we just need to change the implementation of compareTo(Object o) of Student class and the implementation will look like:
🌐
Reddit
reddit.com › r/learnprogramming › [java] how to sort an arraylist of objects, based on objects string field?
r/learnprogramming on Reddit: [Java] How to sort an arraylist of Objects, based on Objects String field?
April 27, 2022 -

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

🌐
Medium
bindushrestha.medium.com › how-to-order-list-of-objects-using-multiple-fields-b0b67c610613
How to sort list of objects using multiple fields? | by Bindu Shrestha | Medium
May 10, 2022 - We can to make use of compare method in Comparator interface to compare using multiple fields. Comparator is a functional interface, which mean it has only one abstract method compare. We are going to override this method in anonymous class. private List<Student> sortWithAnymClass(List<Student> list) {Collections.sort(list, new Comparator<Student>() {// compare using stream and then rollNumber;@Overridepublic int compare(Student s1, Student s2) {if (s1.getStream().equals(s2.getStream())) {if (s1.getRollNum() == s2.getRollNum())return 0;if (s1.getRollNum() > s2.getRollNum())return 1;return -1;}return s1.getStream().compareTo(s2.getStream());}});return list;}
🌐
JavaTechOnline
javatechonline.com › home › how to sort the list in java 8
How To Sort The List In Java 8 - JavaTechOnline
July 14, 2025 - Till now, we have covered Sorting a List of objects by a field/property. Most of the time our field/property is either string, number or an object. Sometimes in a real time project we need to sort a List by Date. Since Date is a special class in Java and exists in multiple formats, it becomes ...
🌐
GeeksforGeeks
geeksforgeeks.org › java › how-to-sort-an-arraylist-of-objects-by-property-in-java
How to Sort an ArrayList of Objects by Property in Java? - GeeksforGeeks
July 23, 2025 - For sorting the list with the given property, we use the list's sort() method. The sort() method takes the list to be sorted (final sorted list is also the same) and a comparator.
🌐
Quora
quora.com › How-do-I-sort-an-ArrayList-of-objects-by-each-objects-integer-parameter
How to sort an ArrayList of objects by each object's integer parameter - Quora
Answer (1 of 4): Well, Java provides a very good API to sort a list of objects, You can implement a Comparator and pass that comparator to Collections.sort along with the list you wanted to sort.
🌐
GeeksforGeeks
geeksforgeeks.org › java › collections-sort-java-examples
Collections.sort() in Java with Examples - GeeksforGeeks
January 4, 2025 - We can use Collections.sort() to sort an array after creating an ArrayList of given array items. ... // Using Collections.sort() to sort an array import java.util.*; public class Collectionsort { public static void main(String[] args) { // create an array of string objs String domains[] = {"Practice", "Geeks", "Code", "Quiz"}; // Here we are making a list named as Collist List colList = new ArrayList(Arrays.asList(domains)); // Collection.sort() method ...
🌐
Java Guides
javaguides.net › 2024 › 09 › java-8-lambda-for-sorting-list-of-objects-by-multiple-fields.html
Java 8 Lambda for Sorting a List of Objects by Multiple Fields
September 9, 2024 - Sorts the list based on multiple fields, such as salary and name. Displays the sorted list. Input: A list of Employee objects with salary and name. Output: A list sorted first by salary (ascending), and if salaries are equal, by name (alphabetically).
🌐
Reddit
reddit.com › r/learnprogramming › sort java arraylist based on an object's attribute​
r/learnprogramming on Reddit: Sort Java ArrayList based on an object's attribute​
December 29, 2018 -

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.

🌐
W3Schools
w3schools.com › java › java_sort_list.asp
Java Sort a List - List Sorting
Another useful class in the java.util package is the Collections class, which include the sort() method for sorting lists alphabetically or numerically. Sort an ArrayList of Strings alphabetically in ascending order:
🌐
C# Corner
c-sharpcorner.com › article › how-can-i-sort-a-list-of-objects-by-property-in-java
How Can I Sort a List of Objects by Property in Java?
October 3, 2025 - In this article, we will learn, in simple terms, how to sort a list of objects by property in Java using various approaches. When we say sorting objects by property, it means arranging them in a particular order (ascending or descending) based on a specific field inside the object.