forked from janbodnar/Java-Advanced
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathListSortIntegers2.java
More file actions
31 lines (20 loc) · 852 Bytes
/
ListSortIntegers2.java
File metadata and controls
31 lines (20 loc) · 852 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
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
// sorting integers by creating a copy of the original
// list, which is intact
public class ListSortIntegers2 {
public static void main(String[] args) {
List<Integer> vals = Arrays.asList(5, -4, 0, 2, -1, 4, 7, 6, 1, -1, 3, 8, -2);
System.out.println("Ascending order");
var sorted1 = vals.stream().sorted().toList();
System.out.println(sorted1);
System.out.println("-------------------------------");
System.out.println("Descending order");
var sorted2 = vals.stream().sorted(Comparator.reverseOrder()).toList();
System.out.println(sorted2);
System.out.println("-------------------------------");
System.out.println("Original order");
System.out.println(vals);
}
}