Fixed the "Lambda Sort" Article

This commit is contained in:
Ali Dehghani 2020-08-12 01:50:52 +04:30
parent e7d3347960
commit 1945c8a312
2 changed files with 2 additions and 2 deletions

View File

@ -37,7 +37,7 @@ public class Human {
public static int compareByNameThenAge(final Human lhs, final Human rhs) { public static int compareByNameThenAge(final Human lhs, final Human rhs) {
if (lhs.name.equals(rhs.name)) { if (lhs.name.equals(rhs.name)) {
return lhs.age - rhs.age; return Integer.compare(lhs.age, rhs.age);
} else { } else {
return lhs.name.compareTo(rhs.name); return lhs.name.compareTo(rhs.name);
} }

View File

@ -54,7 +54,7 @@ public class Java8SortUnitTest {
final List<Human> humans = Lists.newArrayList(new Human("Sarah", 12), new Human("Sarah", 10), new Human("Zack", 12)); final List<Human> humans = Lists.newArrayList(new Human("Sarah", 12), new Human("Sarah", 10), new Human("Zack", 12));
humans.sort((lhs, rhs) -> { humans.sort((lhs, rhs) -> {
if (lhs.getName().equals(rhs.getName())) { if (lhs.getName().equals(rhs.getName())) {
return lhs.getAge() - rhs.getAge(); return Integer.compare(lhs.getAge(), rhs.getAge());
} else { } else {
return lhs.getName().compareTo(rhs.getName()); return lhs.getName().compareTo(rhs.getName());
} }