This is not like Collections.sort() where the parameter reference gets sorted. In this case you just get a sorted stream that you need to collect and assign to another variable eventually:

List result = list.stream().sorted((o1, o2)->o1.getItem().getValue().
                                   compareTo(o2.getItem().getValue())).
                                   collect(Collectors.toList());

You've just missed to assign the result

Answer from Jan B. on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › java › stream-sorted-in-java
Stream sorted() in Java - GeeksforGeeks
January 23, 2026 - import java.util.*; class GFG { public static void main(String[] args) { List<Integer> list = Arrays.asList(-9, -18, 0, 25, 4); list.stream().sorted().forEach(System.out::println); } } ... Paramerter: "T" type of stream elements.
🌐
Medium
medium.com › @AlexanderObregon › javas-stream-sorted-method-explained-52b9b25e9f84
Java’s Stream.sorted() Method Explained | Medium
December 26, 2024 - In this example, the stream is empty, so the sorted() method has no elements to process. While no error occurs, you might want to log or handle cases where no data is available for better user feedback. Streams containing null elements can throw a NullPointerException when using natural order sorting. To handle this, you can filter out null values or provide a comparator that specifies how null should be handled. ... import java.util.Arrays; import java.util.List; public class NullHandlingExample { public static void main(String[] args) { List<String> names = Arrays.asList("Rebecca", null, "Carlos", "Maria"); names.stream() .filter(name -> name != null) // Filter out null values .sorted() .forEach(System.out::println); } }
🌐
HowToDoInJava
howtodoinjava.com › home › java 8 › sorting a stream by multiple fields in java
Sorting a Stream by Multiple Fields in Java - Group by sort example
March 10, 2022 - //first name comparator Comparator<Employee> compareByFirstName = Comparator.comparing( Employee::getFirstName ); //last name comparator Comparator<Employee> compareByLastName = Comparator.comparing( Employee::getLastName ); //Compare by first name and then last name (multiple fields) Comparator<Employee> compareByFullName = compareByFirstName.thenComparing(compareByLastName); //Using Comparator - pseudo code list.stream().sorted( comparator ).collect(); Given below is an example of using thenComparing() to create Comparator which is capable of sorting the employees’ list by their first name
🌐
amitph
amitph.com › home › java › sorting collections with java streams api
Sorting Collections with Java Streams API - amitph
November 22, 2024 - By default, both Java Stream sorted() methods sort the elements in the forward or ascending order. To sort a stream in the descending order of the elements, we need to provide a reverse Comparator. For example, let’s sort a stream of elements in the descending natural order.
🌐
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()); ...
Find elsewhere
🌐
HowToDoInJava
howtodoinjava.com › home › java 8 › java stream sorted() example with/without comparator
Java Stream sorted() Example with/without Comparator
August 23, 2023 - For ordered streams, the sort is stable. For unordered streams, no stability guarantees are made. In the given Java example, we are sorting a List of integers in the natural order and printing them into the standard output.
🌐
ConcretePage
concretepage.com › java › jdk-8 › java-8-stream-sorted-example
Java Stream sorted()
The element class must implement Comparable interface. 2. sorted(Comparator&lt? super T&gt comparator): Here we create an instance of Comparator using lambda expression. We can sort the stream elements in ascending and descending order. The following line of code will sort the list in natural ordering.
🌐
Medium
rameshfadatare.medium.com › java-stream-sorted-method-with-examples-84c7ebf7d056
Java Stream sorted() Method with Examples | by Ramesh Fadatare | Medium
September 26, 2024 - The sorted() method in Java is a part of the java.util.stream.Stream interface. In this guide, we will learn how to use sorted() method in Java with practical examples and real-world use cases to better understand its functionality.
🌐
Educative
educative.io › answers › what-is-streamsorted-method-in-java
What is Stream.sorted() method in Java?
Let's look at the code below: ... Line 5: We create a Employee class with four fields: id, name, salary, and designation. Lines 30 to 35: We create a list of Employee objects and add three elements to the list.
🌐
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.
🌐
Java2Blog
java2blog.com › home › core java › java 8 › java stream sorted example
Java Stream sorted example - Java2Blog
May 9, 2021 - Let’s see complete example to sort list of integers. ... Let’s use Stream’s sorted() to sort the list of Students now. ... Let’s run above program. Exception in thread “main” java.lang.ClassCastException: org.arpit.java2blog.Student cannot be cast to java.lang.Comparable at java.util.Comparators$NaturalOrderComparator.compare(Comparators.java:47) at java.util.TimSort.countRunAndMakeAscending(TimSort.java:355) at java.util.TimSort.sort(TimSort.java:220) at java.util.Arrays.sort(Arrays.java:1512) at java.util.stream.SortedOps$SizedRefSortingSink.end(SortedOps.java:353) at java.util.s
🌐
Medium
medium.com › @rtj1857 › sorting-in-java-using-streams-dc7fd6597872
Sorting in Java using Streams.. We will learn how to sort a list of… | by Ramit Raj | Medium
December 21, 2023 - List<Employee> sortedEmployee= list.stream().filter(Objects::nonNull) .sorted(Comparator.comparing(Employee::getSalary) .reversed() .thenComparing(Employee::getName)) .toList(); So similarly way we can sort till N number of fields where total ...
🌐
GeeksforGeeks
geeksforgeeks.org › java › stream-sorted-comparator-comparator-method-java
Stream sorted (Comparator comparator) method in Java - GeeksforGeeks
December 26, 2025 - Example 2:This code demonstrates how to sort a stream of strings based on their last character using Stream.sorted() with a custom comparator. ... import java.util.stream.Stream; class GFG { public static void main(String[] args) { String[] array = { "GFG", "Geeks", "for", "GeeksforGeeks", "GeeksQuiz" }; Stream.of(array) .sorted((str1, str2) -> Character.compare( str1.charAt(str1.length() - 1), str2.charAt(str2.length() - 1))) .forEach(System.out::println); } }
🌐
HowToDoInJava
howtodoinjava.com › home › java 8 › how to sort a stream in java
How to Sort a Stream in Java
September 16, 2022 - Java programs to sort a stream of strings using Stream.sorted() method in ascending and descending order.
🌐
Javadevcentral
javadevcentral.com › java stream sorted operation
Java Stream sorted operation | Java Developer Central
August 28, 2023 - Let us now look at an example of sorting a stream having elements of a custom class. Shown below is the Student class. Each student has an id and a name. The Student class has a constructor, getter/accessor methods for the fields and toString to print the instance variable values. package com.javadevcentral.stream.sort; public class Student { private final int id; private final String name; public Student(int id, String name) { this.id = id; this.name = name; } public int getId() { return id; } public String getName() { return name; } @Override public String toString() { return "Student{" + "id=" + id + ", name='" + name + '\'' + '}'; } }
🌐
Java Guides
javaguides.net › 2024 › 04 › java-stream-sorted-example.html
Java Stream sorted() Example
April 27, 2024 - ... import java.util.Comparator; ... { public static void main(String[] args) { // Stream of integers Stream<Integer> numberStream = Stream.of(5, 3, 9, 1, 4); // Sort the stream in natural order List<Integer> sortedNumbers = ...