Java SE API
Comparator.comparingInt
Create a comparator from an integer-valued key function.
Formal Syntax & Structure
java syntaxjava
static <T> Comparator<T> comparingInt(ToIntFunction<? super T> keyExtractor)Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| keyExtractor | ToIntFunction<? super T> | Extracts the integer sorting key. |
Return Value
Comparator<T>
Comparator ordered by extracted int.
Working Example
Example Snippetjava
import java.util.*;public class Main { record Job(String code, int priority) {} public static void main(String[] args) { List<Job> jobs = new ArrayList<>(List.of(new Job("C", 2), new Job("B", 5), new Job("A", 5))); Comparator<Job> order = Comparator.comparingInt(Job::priority).reversed().thenComparing(Job::code); jobs.sort(order); for (Job j : jobs) System.out.println(j.code()); }}