Use this method Collections.sort(List,Comparator) . Implement a Comparator and pass it to Collections.sort().
class RecipeCompare implements Comparator<Recipe> {
@Override
public int compare(Recipe o1, Recipe o2) {
// write comparison logic here like below , it's just a sample
return o1.getID().compareTo(o2.getID());
}
}
Then use the Comparator as
Collections.sort(recipes,new RecipeCompare());
Answer from AllTooSir on Stack OverflowGeeksforGeeks
geeksforgeeks.org › java › collections-sort-java-examples
Collections.sort() in Java with Examples - GeeksforGeeks
Collections.sort(List<T> list); Sorts the list in ascending order (natural order) Elements must implement Comparable · Example: Sorting an ArrayList in ascending order · JAVA ·
Published: April 30, 2026
Oracle
docs.oracle.com › javase › 8 › docs › api › java › util › Collections.html
Collections (Java Platform SE 8 )
July 21, 2026 - These algorithms may, but are not required to, throw this exception if an invocation would have no effect on the collection. For example, invoking the sort method on an unmodifiable list that is already sorted may or may not throw UnsupportedOperationException. This class is a member of the Java Collections Framework.
Top answer 1 of 5
64
Use this method Collections.sort(List,Comparator) . Implement a Comparator and pass it to Collections.sort().
class RecipeCompare implements Comparator<Recipe> {
@Override
public int compare(Recipe o1, Recipe o2) {
// write comparison logic here like below , it's just a sample
return o1.getID().compareTo(o2.getID());
}
}
Then use the Comparator as
Collections.sort(recipes,new RecipeCompare());
2 of 5
29
The answer given by NINCOMPOOP can be made simpler using Lambda Expressions:
Collections.sort(recipes, (Recipe r1, Recipe r2) ->
r1.getID().compareTo(r2.getID()));
Also introduced after Java 8 is the comparator construction methods in the Comparator interface. Using these, one can further reduce this to 1:
recipes.sort(comparingInt(Recipe::getId));
1 Bloch, J. Effective Java (3rd Edition). 2018. Item 42, p. 194.
How to do in Java
howtodoinjava.com › home › java sorting › java collections.sort(): natural order, comparator, stability
Java Collections.sort(): Natural Order, Comparator, Stability
July 21, 2018 - In both cases, the algorithm is TimSort, a stable merge sort that takes O(n log n) comparisons on random data and about n comparisons on data that is already almost sorted. // does not compile: JDK source of Collections (excerpt) public static <T extends Comparable<? super T>> void sort(List<T> list) { list.sort(null); } public static <T> void sort(List<T> list, Comparator<? super T> c) { list.sort(c); }
TutorialsPoint
tutorialspoint.com › java › util › collections_sort_comparable.htm
Java Collections sort() Method
The Java Collections sort(List<T>) method is used to sort the specified list into ascending order, according to the natural ordering of its element.
Interview Kickstart
interviewkickstart.com › home › blogs › learn › collections sort in java
Collections Sort in Java | Interview Kickstart
September 25, 2024 - Collections sort is a method of Java Collections class used to sort a list, which implements the List interface. All the elements in the list must be mutually comparable. If a list consists of string elements, then it will be sorted in alphabetical ...
CodeGym
codegym.cc › java blog › java collections › java collections sort()
Java Collections sort()
October 11, 2023 - Thankfully, Collections.sort()is overwritten with an optional 2nd parameter, which allows you to do just this: sort(List l, Comparator c) But what’s a comparator? Well, a comparator is simply a function that compares two inputs and returns a number representing which input comes first. If you are sorting an ArrayList of primitive data types, then Java Collections already provides you with a reverseOrder() comparator.
Codecademy
codecademy.com › docs › java › collections › .sort()
Java | Collections | .sort() | Codecademy
July 18, 2022 - import java.util.*; // This sorts by natural ordering Collections.sort(myList); // This sorts by a Comparator Collections.sort(myList, myComparator);
ZetCode
zetcode.com › java › collections-sort
Java Collections.sort - Complete Tutorial with Examples
The Collections.sort method is a utility method in Java's java.util.Collections class. It provides functionality to sort elements in a List.
Baeldung
baeldung.com › home › java › java collections › difference between arrays.sort() and collections.sort()
Difference Between Arrays.sort() and Collections.sort() | Baeldung
March 7, 2025 - Whether we’re working with numerical data or alphabetical strings, Arrays.sort() can arrange the elements in ascending order. Additionally, we can modify the behavior with custom comparators for object arrays. This method is part of the java.util.Arrays class, which provides a suite of utilities for array manipulation. On the other hand, Collections.sort() is designed for sorting instances of the List interface in Java’s Collection Framework.
YouTube
youtube.com › selenium express
How to sort a collection in java | Collections.sort() | sorting a List | Java interview question |#1 - YouTube
This video is an introduction to java comparable vs comparator series. In this video, we will understand the basics to use the sort() of the Collections clas...
Published: November 23, 2019
Views: 6K