This method returns a Comparator which compares the objects according to the keyExtractor function. The extractor function extracts an int sort key from the object of type T.
package com.logicbig.example.comparator;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class ComparingIntExample {
public static void main(String... args) {
List<Person> list = createExamplePersons();
System.out.printf("before sort: %s%n", list);
Collections.sort(list, Comparator.comparingInt(Person::getAge));
System.out.printf("after sort: %s%n", list);
}
private static List<Person> createExamplePersons() {
return Arrays.asList(
new Person("John", 33),
new Person("Sara", 28),
new Person("MIke", 30)
);
}
private static class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
}
Output
before sort: [Person{name='John', age=33}, Person{name='Sara', age=28}, Person{name='MIke', age=30}]
after sort: [Person{name='Sara', age=28}, Person{name='MIke', age=30}, Person{name='John', age=33}]