Skip to content

Commit f19db96

Browse files
authored
Create ListSortIntegers2.java
1 parent 79884cc commit f19db96

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

comparesort/ListSortIntegers2.java

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import java.util.Arrays;
2+
import java.util.Comparator;
3+
import java.util.List;
4+
5+
// sorting integers by creating a copy of the original
6+
// list, which is intact
7+
8+
public class ListSortIntegers2 {
9+
10+
public static void main(String[] args) {
11+
12+
List<Integer> vals = Arrays.asList(5, -4, 0, 2, -1, 4, 7, 6, 1, -1, 3, 8, -2);
13+
14+
System.out.println("Ascending order");
15+
16+
var sorted1 = vals.stream().sorted().toList();
17+
System.out.println(sorted1);
18+
19+
System.out.println("-------------------------------");
20+
21+
System.out.println("Descending order");
22+
23+
var sorted2 = vals.stream().sorted(Comparator.reverseOrder()).toList();
24+
System.out.println(sorted2);
25+
26+
System.out.println("-------------------------------");
27+
28+
System.out.println("Original order");
29+
System.out.println(vals);
30+
}
31+
}

0 commit comments

Comments
 (0)