55
66public class Sorting {
77
8- public static void main (String ...args ){
8+ public static void main (String ...args ){
99
1010 // 1
11- List <Apple > inventory = new ArrayList <>();
12- inventory .addAll (Arrays .asList (new Apple (80 ,"green" ), new Apple (155 , "green" ), new Apple (120 , "red" )));
11+ List <Apple > inventory = new ArrayList <>();
12+ inventory .addAll (Arrays .asList (new Apple (80 ,"green" ), new Apple (155 , "green" ), new Apple (120 , "red" )));
1313
14- // [Apple{color='green', weight=80}, Apple{color='red', weight=120}, Apple{color='green', weight=155}]
15- inventory .sort (new AppleComparator ());
16- System .out .println (inventory );
14+ // [Apple{color='green', weight=80}, Apple{color='red', weight=120}, Apple{color='green', weight=155}]
15+ inventory .sort (new AppleComparator ());
16+ System .out .println (inventory );
1717
1818 // reshuffling things a little
1919 inventory .set (1 , new Apple (30 , "green" ));
2020
2121 // 2
2222 // [Apple{color='green', weight=30}, Apple{color='green', weight=80}, Apple{color='green', weight=155}]
2323 inventory .sort (new Comparator <Apple >() {
24- public int compare (Apple a1 , Apple a2 ){
25- return a1 .getWeight ().compareTo (a2 .getWeight ());
26- }
27- });
28- System .out .println (inventory );
24+ public int compare (Apple a1 , Apple a2 ){
25+ return a1 .getWeight ().compareTo (a2 .getWeight ());
26+ }});
27+ System .out .println (inventory );
2928
3029 // reshuffling things a little
3130 inventory .set (1 , new Apple (20 , "red" ));
@@ -42,44 +41,44 @@ public int compare(Apple a1, Apple a2){
4241 // [Apple{color='red', weight=10}, Apple{color='red', weight=20}, Apple{color='green', weight=155}]
4342 inventory .sort (comparing (Apple ::getWeight ));
4443 System .out .println (inventory );
45- }
44+ }
4645
47- public static class Apple {
48- private Integer weight = 0 ;
49- private String color = "" ;
46+ public static class Apple {
47+ private Integer weight = 0 ;
48+ private String color = "" ;
5049
51- public Apple (Integer weight , String color ){
52- this .weight = weight ;
53- this .color = color ;
54- }
50+ public Apple (Integer weight , String color ){
51+ this .weight = weight ;
52+ this .color = color ;
53+ }
5554
56- public Integer getWeight () {
57- return weight ;
58- }
55+ public Integer getWeight () {
56+ return weight ;
57+ }
5958
60- public void setWeight (Integer weight ) {
61- this .weight = weight ;
62- }
59+ public void setWeight (Integer weight ) {
60+ this .weight = weight ;
61+ }
6362
64- public String getColor () {
65- return color ;
66- }
63+ public String getColor () {
64+ return color ;
65+ }
6766
68- public void setColor (String color ) {
69- this .color = color ;
70- }
67+ public void setColor (String color ) {
68+ this .color = color ;
69+ }
7170
72- public String toString () {
73- return "Apple{" +
74- "color='" + color + '\'' +
75- ", weight=" + weight +
76- '}' ;
77- }
78- }
71+ public String toString () {
72+ return "Apple{" +
73+ "color='" + color + '\'' +
74+ ", weight=" + weight +
75+ '}' ;
76+ }
77+ }
7978
80- static class AppleComparator implements Comparator <Apple > {
81- public int compare (Apple a1 , Apple a2 ){
82- return a1 .getWeight ().compareTo (a2 .getWeight ());
83- }
84- }
85- }
79+ static class AppleComparator implements Comparator <Apple > {
80+ public int compare (Apple a1 , Apple a2 ){
81+ return a1 .getWeight ().compareTo (a2 .getWeight ());
82+ }
83+ }
84+ }
0 commit comments