Assuming you actually have a List<AnObject>, all you need is

list.sort(Comparator.comparing(a -> a.attr));

If you make you code clean by not using public fields, but accessor methods, it becomes even cleaner:

list.sort(Comparator.comparing(AnObject::getAttr));
Answer from JB Nizet on Stack Overflow
๐ŸŒ
JavaTechOnline
javatechonline.com โ€บ home โ€บ how to sort the list in java 8
How To Sort The List In Java 8 - JavaTechOnline
January 29, 2026 - Here, we will cover almost all commonly used ways to implement sorting and comparison especially using Java 8. When we use the approaches of sorting a list using Java 8, we can save too many lines of code for sure. When we have custom objects to sort then we have to provide the comparator implementation, we can replace it with lambda to make the code shorter and more readable.
๐ŸŒ
Java67
java67.com โ€บ 2021 โ€บ 09 โ€บ 3-ways-to-sort-list-in-java-8-and-11.html
3 ways to sort a List in Java 8 and 11 - Example Tutorial | Java67
List<String> names = ... using Java8 List.sort(): [Mary, Ken, Joe, James] You can see from the output that Mary comes first and James comes last, which confirms that elements are sorted in the opposite of lexicographic or alphabetic order. Sometimes, you have a list of objects which cannot ...
๐ŸŒ
BeginnersBook
beginnersbook.com โ€บ 2017 โ€บ 10 โ€บ java-8-lambda-comparator-example-for-sorting-list-of-custom-objects
Java 8 Lambda Comparator example for Sorting List of Custom Objects
Here we are sorting a list of objects of Student class by name. Comparator sortingByName = new Comparator() { @Override public int compare(Student s1, Student s2) { return s1.getName().compareTo(s2.getName()); } }; Using Lambda expression: The Java 8 equivalent code using Lambda expression would look like this:
๐ŸŒ
Blogger
javarevisited.blogspot.com โ€บ 2023 โ€บ 10 โ€บ 6-ways-to-sort-arraylist-in-java.html
6 ways to sort ArrayList in Java 8? List.sort() + Collections.sort() Example Tutorial
October 4, 2023 - Though, my preferred way is Collections.sort() method because it is available to all the Java versions and its easy to use and it also allows you to sort in any custom order. The method is overloaded which gives you ability to sort a list of ...
๐ŸŒ
Stack Abuse
stackabuse.com โ€บ java-8-how-to-sort-list-with-stream-sorted
Java 8 โ€“ How to Sort List with Stream.sorted()
July 21, 2021 - In this tutorial guide, we'll cover everything you need to know about the Stream.sorted() API. We'll sort integers, strings and custom objects in ascending and descending order as well as define custom comparators in Java.
๐ŸŒ
Mkyong
mkyong.com โ€บ home โ€บ java8 โ€บ java 8 โ€“ how to sort list with stream.sorted()
Java 8 - How to sort list with stream.sorted() - Mkyong.com
March 8, 2019 - /*List<User> sortedList = users.stream() .sorted((o1, o2) -> o1.getName().compareTo(o2.getName())) .collect(Collectors.toList());*/ List<User> sortedList = users.stream() .sorted(Comparator.comparing(User::getName)) .collect(Collectors.toList()); ... Founder of Mkyong.com, passionate Java and open-source technologies.
Find elsewhere
๐ŸŒ
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 - List.sort() is an instance method of the List class and it sorts the elements in the order defined by their natural ordering or by a specified Icomparer implementation. In Java 8 and above, you can use the Stream API to sort a list.
๐ŸŒ
Medium
medium.com โ€บ @shivamtyagicool โ€บ java-object-sorting-techniques-comparator-and-modern-java-features-309e4390a7ca
Java Object Sorting Techniques: Comparator and Modern Java Features | by Shivam Tyagi | Medium
August 29, 2024 - Java Object Sorting Techniques: Comparator and Modern Java Features In Java 8, you can use the Comparator interface along with lambda expressions or method references to sort a list of objects. The โ€ฆ
๐ŸŒ
Medium
medium.com โ€บ @AlexanderObregon โ€บ sorting-lists-in-java-using-collections-sort-14b150ac8682
Sorting Lists in Java Using Collections.sort | Medium
June 4, 2025 - Sorting is a basic task in any programming language, but the way itโ€™s done can reveal a lot about how things work behind the scenes. Java provides several tools for sorting, but one of the most commonly used is Collections.sort(). It works with lists of numbers, strings, or even custom objects when you provide it with a way to compare them.
๐ŸŒ
Java67
java67.com โ€บ 2017 โ€บ 07 โ€บ how-to-sort-arraylist-of-objects-using.html
How to Sort ArrayList of Objects by Fields in Java? Custom + Reversed Order Sorting Comparator Example | Java67
These Comparator implementation classes use Java 8 lambdas to implement compare() method as shown here, and sort the list of objects into ascending order of title and fee.
๐ŸŒ
Medium
medium.com โ€บ javarevisited โ€บ sorting-a-list-in-java-8-551d20ad102e
Sorting a List in Java 8. How many options do we have to sort aโ€ฆ | by Suraj Mishra | Javarevisited | Medium
December 15, 2022 - In the below example, we are sorting the list of customers by their age. Service class returns an immutable list, but Collections.sort() expects mutable so that it can perform sorting list hence we need to convert Immutable list to mutable. ... A humble place to learn Java and Programming better.
๐ŸŒ
Javatpoint
javatpoint.com โ€บ how-to-sort-a-list-in-java
How to Sort a List in Java - Javatpoint
How to Sort a List in Java with oops, string, exceptions, multithreading, collections, jdbc, rmi, fundamentals, programs, swing, javafx, io streams, networking, sockets, classes, objects etc,
๐ŸŒ
How to do in Java
howtodoinjava.com โ€บ home โ€บ java sorting โ€บ java collections sort()
Java Collections sort() - HowToDoInJava
December 14, 2022 - Learn to use Collections.sort() method to sort a list of objects using some examples. By default, the sort() method sorts a given list into ascending order (or natural order). We can use Collections.reverseOrder() method, which returns a Comparator, ...
๐ŸŒ
Medium
kevalpadsumbiya.medium.com โ€บ custom-sorting-of-list-of-objects-using-java-stream-b24d93e6e71e
Custom sorting of list of objects using Java Stream | Medium
February 13, 2023 - package com.custom.sorting.service; import com.custom.sorting.model.Seller; import java.util.Arrays; import java.util.Comparator; import java.util.List; import java.util.stream.Collectors; public class CustomSorting { public static void main(String[] args) { List<Seller> sellerList = getSampleListOfSellers(); //seller name comparator Comparator<Seller> sellerNameComparator = Comparator.comparing(Seller::getSellerName); //seller location distance comparator Comparator<Seller> sellerLocationDistanceComparator = Comparator.comparing(Seller::getDistanceFromMyLocation); //seller state comparator Co
๐ŸŒ
DZone
dzone.com โ€บ coding โ€บ java โ€บ java 8 comparator: how to sort a list
Java 8 Comparator: How to Sort a List
May 20, 2019 - In the example, we want to sort the list so that we have starred movies at the top of the List. List<Movie> movies = Arrays.asList( new Movie("Lord of the rings", 8.8, true), new Movie("Back to the future", 8.5, false), new Movie("Carlito's way", 7.9, true), new Movie("Pulp fiction", 8.9, false)); movies.sort(new Comparator<Movie>() { @Override public int compare(Movie m1, Movie m2) { if(m1.getStarred() == m2.getStarred()){ return 0; } return m1.getStarred() ?
๐ŸŒ
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 - Suppose we didnโ€™t want to modify the original list, but return a new sorted list. In this situation, we can use the sorted() method from the Stream interface introduced in Java 8.
๐ŸŒ
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 - In Java 8, lambda expressions make this type of sorting simple and concise. You can chain multiple Comparator conditions using thenComparing() to sort by more than one field. ... Defines a list of Employee objects.
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ java โ€บ java collections โ€บ sorting in java
Sorting in Java | Baeldung
February 3, 2026 - Note that we can use Collections.sort() in a similar fashion to sort List and Set of Objects in Natural or Custom order as described above for Arrays. Start with Java 8, we can use Lambdas to implement the Comparator Functional Interface.