forked from janbodnar/Java-Advanced
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathListSort.java
More file actions
53 lines (36 loc) · 1.04 KB
/
ListSort.java
File metadata and controls
53 lines (36 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package com.zetcode;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
class Person {
private int age;
private String name;
public Person(int age, String name) {
this.age = age;
this.name = name;
}
public int getAge() {
return age;
}
@Override
public String toString() {
return "Age: " + age + " Name: " + name;
}
}
public class ListSort {
public static void main(String[] args) {
List<Person> persons = createList();
persons.sort(Comparator.comparing(Person::getAge).reversed());
System.out.println(persons);
}
private static List<Person> createList() {
List<Person> persons = new ArrayList<>();
persons.add(new Person(17, "Jane"));
persons.add(new Person(32, "Peter"));
persons.add(new Person(47, "Patrick"));
persons.add(new Person(22, "Mary"));
persons.add(new Person(39, "Robert"));
persons.add(new Person(54, "Greg"));
return persons;
}
}