W3Schools
w3schools.com โบ java โบ ref_arraylist_sort.asp
Java ArrayList sort() Method
Non-primitive types must implement Java's Comparable interface in order to be sorted without a comparator. ... import java.util.ArrayList; public class Main { public static void main(String[] args) { ArrayList<String> cars = new ArrayList<String>(); cars.add("Volvo"); cars.add("BMW"); cars.add("Ford"); cars.add("Mazda"); cars.sort( (a, b) -> { return -1 * a.compareTo(b); } ); System.out.println(cars); } }
15:58
Java 8 Stream - Sort List (ArrayList) in Ascending and Descending ...
16:00
Java 8 Stream #3 - Sorting with Comparator Example - YouTube
16:58
Java 8 Lambda - Sort List (ArrayList) in Ascending and Descending ...
12:05
Java Sort an Array of Objects using Comparator | Lambda Expression ...
17:43
Java 8 Stream - How to Sort a List using lambda | Example | Java ...
Java Training School
javatrainingschool.com โบ home โบ how to sort an arraylist in java
How to sort an Arraylist in java - Java Training School
May 6, 2024 - Functional programming which was introduced in java 8 has made it very easy to sort arraylist using forEach method and lambda expression.
Java Code Geeks
javacodegeeks.com โบ home โบ core java
Sorting ArrayList in Reverse or Descending Order in Java 8 - Java Code Geeks
March 3, 2021 - Next, look at the another way to sort the arraylist in descending order using two methods as below. Collections.sort(arraylist); โ> first sorts the list in the ascending order ยท Collections.reverse(arraylist); โ> Next, reverse the sorted list.
BeginnersBook
beginnersbook.com โบ 2013 โบ 12 โบ how-to-sort-arraylist-in-java
How to sort ArrayList in Java
Here, we are sorting an ArrayList ... //Before sorting System.out.println("Before Sorting: "+ arraylist); // Sorting the list of integers using sort() method Collections.sort(arraylist); //After sorting System.out.println("After Sorting: "+ arraylist); } }...
Java Code Geeks
examples.javacodegeeks.com โบ home โบ java development โบ core java
Sorting Java ArrayList in Ascending and Descending Order - Java Code Geeks
October 23, 2023 - The output from running the updated ... order by using the stream() method to convert the ArrayList into a stream, and then using the sorted() method to sort the elements, and finally collect the sorted elements back into a list...
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
Btw, this is not the only way to sort an ArrayList of objects in Java. If you want you can use Comparable to sort in the natural order and in the decreasing order of natural order imposed by Comparator. the JDK 8 release also added a couple of methods on both java.util.Comparator class and java.util.List to make sorting easier.
Javatpoint
javatpoint.com โบ how-to-sort-arraylist-in-java
How to Sort ArrayList in Java - javatpoint
How to Sort ArrayList in Java with java tutorial, features, history, variables, object, class, programs, operators, for-loop, oops concept, inheritance, array, string, map, math, methods, examples etc.
Java67
java67.com โบ 2012 โบ 08 โบ how-to-sort-arraylist-in-java-list.html
How to sort ArrayList in Java? Examples | Java67
Here is a complete code example of How to sort ArrayList in Java; in this Sorting, we have used Comparable method of String for sorting String on their natural order, You can also use Comparator in place of Comparable to sort String on any other order than natural ordering like in reverse order by using Collections.reverseOrder() or insensitive order by using String.CASE_INSENSITIVE_COMPARATOR.
Top answer 1 of 12
75
For strings this would work
arrayList.sort((p1, p2) -> p1.compareTo(p2));
2 of 12
33
Are you just sorting Strings? If so, you don't need lambdas; there's no point. You just do
import static java.util.Comparator.*;
list.sort(naturalOrder());
...though if you're sorting objects with a String field, then it makes somewhat more sense:
list.sort(comparing(Foo::getString));
Top answer 1 of 16
696
Collections.sort(testList);
Collections.reverse(testList);
That will do what you want. Remember to import Collections though!
Here is the documentation for Collections.
2 of 16
184
Descending:
Collections.sort(mArrayList, new Comparator<CustomData>() {
@Override
public int compare(CustomData lhs, CustomData rhs) {
// -1 - less than, 1 - greater than, 0 - equal, all inversed for descending
return lhs.customInt > rhs.customInt ? -1 : (lhs.customInt < rhs.customInt) ? 1 : 0;
}
});