forked from paulnguyen/code
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMain.java
More file actions
37 lines (23 loc) · 924 Bytes
/
Main.java
File metadata and controls
37 lines (23 loc) · 924 Bytes
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
import java.util.*;
import java.util.function.*;
public class Main {
/*
FROM: Core Java® for the Impatient
Suppose sometimes we want to sort an array of strings in ascending order
and other times in descending order. We can make a method that produces
the correct comparator:
*/
public static Comparator<String> compareInDirecton(int direction) {
return (x, y) -> direction * x.compareTo(y);
}
public static Comparator<String> reverse(Comparator<String> comp) {
return (x, y) -> -comp.compare(x, y);
}
public static void main(String[] args) {
String[] words = { "Mary", "had", "a", "little", "lamb" };
Arrays.sort(words, compareInDirecton(-1));
System.out.println(Arrays.toString(words));
Arrays.sort(words, reverse(String::compareToIgnoreCase));
System.out.println(Arrays.toString(words));
}
}