(a, b) -> Integer.compare(a.score, b.score)

would work. int is not an object, it is a primitive, and you can't call int.compareTo, or any other methods on int.

Even better than that would be

Comparator.comparingInt(s -> s.score)

or, with a getter,

Comparator.comparingInt(Student::getScore)

And using List.sort doesn't make a difference as to whether or not you use lambdas or whatever. You just write personList.sort(Comparator.comparingInt(s -> s.score)) instead of Collections.sort(personList, Comparator.comparingInt(s -> s.score)).

Answer from Louis Wasserman on Stack Overflow
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ java โ€บ java โ€“ powerful comparison with lambdas
Java โ€“ Powerful Comparison with Lambdas | Baeldung
January 8, 2024 - Tips and best practices on using Java 8 lambdas and functional interfaces. ... public class Human { private String name; private int age; // standard constructors, getters/setters, equals and hashcode } Before Java 8, sorting a collection would involve creating an anonymous inner class for the Comparator used in the sort:
๐ŸŒ
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
Using Lambda expression: The Java 8 equivalent code using Lambda expression would look like this: Comparator sortingByName = (Student s1, Student s2)->s1.getName().compareTo(s2.getName()); Note: In Java 8, the List interface supports the sort() method so you need not to use the Comparator.sort(), ...
๐ŸŒ
CodeJava
codejava.net โ€บ java-core โ€บ the-java-language โ€บ java-8-lambda-collections-comparator-example
How to use Java Lambda expression for sorting a List using comparator
Before sorting: [Head First Java-38.9, Thinking in Java-30.0, Effective Java-50.0, Code Complete-42.5] After sorting by title: [Code Complete-42.5, Effective Java-50.0, Head First Java-38.9, Thinking in Java-30.0] After sorting by price descending: [Effective Java-50.0, Code Complete-42.5, Head First Java-38.9, Thinking in Java-30.0] After sorting by price ascending: [Thinking in Java-30.0, Head First Java-38.9, Code Complete-42.5, Effective Java-50.0] Related Tutorials:
๐ŸŒ
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 - Sorting by multiple fields in Java 8 is simple and flexible with lambda expressions and the Comparator interface. You can sort by any number of fields using Comparator.comparing() and thenComparing(), and you can control the sort order (ascending ...
๐ŸŒ
Java Guides
javaguides.net โ€บ 2020 โ€บ 04 โ€บ java-8-lambda-sort-list-in-ascending-and-descending-order.html
Java 8 Lambda - Sort List in Ascending and Descending Order | Comparator Example
September 14, 2020 - In this example, we will see how to sort a list of employees by name in ascending and descending order using Lambda Expressions: package com.java.tutorials.sorting; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; public class SortList { public static void main(String[] args) { List < Employee > employees = new ArrayList < Employee > (); employees.add(new Employee(10, "Ramesh", 30, 400000)); employees.add(new Employee(20, "Santosh", 29, 350000)); employees.add(new Employee(30, "Sanjay", 30, 450000)); employees.add(new Employee(40, "P
๐ŸŒ
w3resource
w3resource.com โ€บ java-exercises โ€บ lambda โ€บ java-lambda-exercise-20.php
Java Program: Sort list of objects with lambda expression
Learn how to use a lambda expression in Java to sort a list of objects based on a specific attribute. Implement custom sorting logic for your objects.
Find elsewhere
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ java-lambda-expression-with-collections
Java Lambda Expression with Collections - GeeksforGeeks
July 11, 2025 - Sorting elements of List(I) with Lambda ยท Expression: - Using lambda expression in place of comparator object for defining our own sorting in collections. ... import java.util.*; public class Demo { public static void main(String[] args) { ArrayList<Integer> al = new ArrayList<Integer>(); al.add(205); al.add(102); al.add(98); al.add(275); al.add(203); System.out.println("Elements of the ArrayList " + "before sorting : " + al); // using lambda expression in place of comparator object Collections.sort(al, (o1, o2) -> (o1 > o2) ?
๐ŸŒ
DZone
dzone.com โ€บ coding โ€บ java โ€บ using lambda expression to sort a list in java 8 using netbeans lambda support
Using Lambda Expression to Sort a List in Java 8 using NetBeans Lambda Support
March 21, 2013 - One can make it even more concise by removing the type information from the parameters as the type information is inferred from the context in which the lambda expression is being used. //Lambda expression with type information removed. Collections.sort(personList, (p1, p2) -> p1.firstName.compareTo(p2.firstName));
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ can-we-sort-a-list-with-lambda-in-java
Can we sort a list with Lambda in Java?
import java.util.Arrays; import java.util.Collections; import java.util.List; public class Demo { public static void main(String... args) { List<String> list = Arrays.asList("LCD","Laptop", "Mobile", "Device", "LED", "Tablet"); System.out.println("List = "+list); Collections.sort(list, (String str1, String str2) -> str2.compareTo(str1)); System.out.println("Sorted List = "+list); } } List = [LCD, Laptop, Mobile, Device, LED, Tablet] Sorted List = [Tablet, Mobile, Laptop, LED, LCD, Device]
๐ŸŒ
Medium
medium.com โ€บ thefreshwrites โ€บ how-to-use-sort-in-java-using-traditional-and-lambda-expression-c95257ff827f
How to use Sort() in Java using Traditional and Lambda Expression? | by Amar Balu | The Fresh Writes | Medium
April 19, 2023 - public class sample { public static void main(String[] args) { List<String> list = new ArrayList<>(); list.add("Apple"); list.add("Subway"); list.add("Mexico"); list.add("Lemon"); list.add("Life"); Integer arr[] = { 2, 31, 4, 23, 453, 232, 2121, 34 }; Arrays.sort(arr); System.out.println("Arrays Sort in Natural Order"); for (int i : arr) { System.out.print(i + " "); } Arrays.sort(arr, Collections.reverseOrder()); System.out.println("Arrays Sort in Reverse Order"); for (int i : arr) { System.out.print(i + " "); }โ€ฆ ... I'm a front-end developer. I love React and Redux, Java, and Data Science.
๐ŸŒ
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 - 1.3 Sort by name ยท /*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()); Java 8 Lambda : Comparator example ยท
๐ŸŒ
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
๐ŸŒ
Java8
java8.org โ€บ sort-a-collection-using-lambda-comparators
Sort a collection with lambda comparator - Java Resources
With Java 8 we can use a lambda ... persons = new ArrayList<>(); // Using a lambda expression to implement the Comparator Collections.sort(persons, (p1, p2) -> p1.name.compareTo(p2.name));...