Skip to content

Commit 1776d12

Browse files
authored
Create ListSortSurname.java
1 parent c6a0d58 commit 1776d12

1 file changed

Lines changed: 26 additions & 0 deletions

File tree

comparesort/ListSortSurname.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.zetcode;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.Collections;
6+
import java.util.Comparator;
7+
import java.util.List;
8+
import java.util.function.BiFunction;
9+
import java.util.function.Function;
10+
11+
// sorting a list of full names by surname
12+
13+
public class ListSortSurname {
14+
15+
public static void main(String[] args) {
16+
17+
var names = Arrays.asList("John Doe", "Lucy Smith",
18+
"Benjamin Young", "Robert Brown", "Thomas Moore",
19+
"Linda Black", "Adam Smith", "Jane Smith");
20+
21+
Function<String, String> fun = (String fullName) -> fullName.split("\s")[1];
22+
names.sort(Comparator.comparing(fun).reversed());
23+
24+
System.out.println(names);
25+
}
26+
}

0 commit comments

Comments
 (0)