66import java .util .List ;
77
88public class Company {
9- private List <Human > employees ;
9+ public final List <Human > employees ;
1010
1111 public Company () {
12- this .employees = new ArrayList <>();
13- this .employees .add (new Human (80 ));
14- this .employees .add (new Human (10 ));
15- this .employees .add (new Human (65 ));
12+ this .employees = new ArrayList <>(List .of (new Human (80 ), new Human (10 ), new Human (65 )));
1613 }
1714
1815 public void sortByAgeDescending () {
@@ -24,32 +21,36 @@ public void sortByAgeDescending() {
2421 employees ,
2522 new Comparator <Human >() {
2623 public int compare (Human h1 , Human h2 ) {
27- if (h1 .age () > h1 .age ()) {
24+ if (h1 .age () < h2 .age ()) {
25+ return -1 ;
26+ } else if (h1 .age () > h2 .age ()) {
2827 return 1 ;
2928 } else {
30- return - 1 ;
29+ return 0 ;
3130 }
3231 }
3332 });
3433
35- // Lambda Funktion - Lange Syntax
34+ // // Lambda Funktion - Lange Syntax
3635 Collections .sort (
3736 employees ,
3837 (Human h1 , Human h2 ) -> {
39- if (h1 .age () > h1 .age ()) {
38+ if (h1 .age () < h2 .age ()) {
39+ return -1 ;
40+ } else if (h1 .age () > h2 .age ()) {
4041 return 1 ;
4142 } else {
42- return - 1 ;
43+ return 0 ;
4344 }
4445 });
45- // Lambda Funktion - Kurze Syntax
46- Collections .sort (employees , (Human h1 , Human h2 ) -> h1 .age () > h1 .age () ? 1 : - 1 );
46+ // // Lambda Funktion - Kurze Syntax
47+ Collections .sort (employees , (Human h1 , Human h2 ) -> h1 .age () < h2 .age () ? - 1 : h1 . age () > h2 . age () ? 1 : 0 );
4748
48- // Lambda Funktion - Kurze Syntax ohne explizite Datentypen
49- Collections .sort (employees , (h1 , h2 ) -> h1 .age () > h1 .age () ? 1 : - 1 );
49+ // // Lambda Funktion - Kurze Syntax ohne explizite Datentypen
50+ Collections .sort (employees , (h1 , h2 ) -> h1 .age () < h2 .age () ? - 1 : h1 . age () > h2 . age () ? 1 : 0 );
5051
51- // Lambda Funktion - Als Referenzvariable gespeichert
52- Comparator <Human > ageSorter = (h1 , h2 ) -> h1 .age () > h1 .age () ? 1 : - 1 ;
52+ // // Lambda Funktion - Als Referenzvariable gespeichert
53+ Comparator <Human > ageSorter = (h1 , h2 ) -> h1 .age () < h2 .age () ? - 1 : h1 . age () > h2 . age () ? 1 : 0 ;
5354 Collections .sort (employees , ageSorter );
5455 }
5556}
0 commit comments