(a, b) -> Integer.compare(a.score, b.score)
would work. int is not an object, it is a primitive, and you can't call int.compareTo, or any other methods on int.
Even better than that would be
Comparator.comparingInt(s -> s.score)
or, with a getter,
Comparator.comparingInt(Student::getScore)
And using List.sort doesn't make a difference as to whether or not you use lambdas or whatever. You just write personList.sort(Comparator.comparingInt(s -> s.score)) instead of Collections.sort(personList, Comparator.comparingInt(s -> s.score)).
Videos
For strings this would work
arrayList.sort((p1, p2) -> p1.compareTo(p2));
Are you just sorting Strings? If so, you don't need lambdas; there's no point. You just do
import static java.util.Comparator.*;
list.sort(naturalOrder());
...though if you're sorting objects with a String field, then it makes somewhat more sense:
list.sort(comparing(Foo::getString));
The cleanest way would be:
CopyArrays.sort(months, Comparator.comparingInt(String::length));
or, with a static import:
CopyArrays.sort(months, comparingInt(String::length));
However, this would work too but is more verbose:
CopyArrays.sort(months,
(String a, String b) -> a.length() - b.length());
Or shorter:
CopyArrays.sort(months, (a, b) -> a.length() - b.length());
Finally your last one:
CopyArrays.sort(months,
(String a, String b) -> { return Integer.signum(a.length() - b.length()) };
);
has the ; misplaced - it should be:
CopyArrays.sort(months,
(String a, String b) -> { return Integer.signum(a.length() - b.length()); }
);
You're looking for this:
CopyArrays.sort(months, (a, b) -> Integer.signum(a.length() - b.length()));
A lambda comparator would be something like this.
Comparator<Car> comp = (c1, c2)-> c1.getName().compareTo(c2.getName());
In the above example, the Comparator is comparing on a specific field of the Car class, name. Since name probably returns a string, one can use compareTo since the String class implements the Comparable (not Comparator) interface for comparing Strings.
But the lambda could be specified much more easily using one of the methods in the Comparator interface. The comparing method may take a lambda or a method reference (shown below).
Comparator<Car> comp = Comparator.comparing(Car::getName);
Then when it is passed to the sort method, the sort method will apply it to the objects under sort as comp.compare(obj1, obj2) (although it may not be called comp in the method that uses it)
For more information, check out The Java Tutorials
Take a look at what lambda is here.
If you have a list in your main class, it's as simple as just sorting it. You probably don't even need the class LambdaComparator.
List<Car> list = new ArrayList<>();
Collections.sort(list, (a, b) -> a.getName().compareTo(b.getName()));
Assuming you actually have a List<AnObject>, all you need is
list.sort(Comparator.comparing(a -> a.attr));
If you make you code clean by not using public fields, but accessor methods, it becomes even cleaner:
list.sort(Comparator.comparing(AnObject::getAttr));
As a complement to @JB Nizet's answer, if your attr is nullable,
list.sort(Comparator.comparing(AnObject::getAttr));
may throw a NPE.
If you also want to sort null values, you can consider
list.sort(Comparator.comparing(a -> a.attr, Comparator.nullsFirst(Comparator.naturalOrder())));
or
list.sort(Comparator.comparing(a -> a.attr, Comparator.nullsLast(Comparator.naturalOrder())));
which will put nulls first or last.