Skip to content

Commit ab16164

Browse files
committed
Fixed the "Lambda Sort" Article
1 parent f80f13e commit ab16164

2 files changed

Lines changed: 2 additions & 2 deletions

File tree

core-java-modules/core-java-lambdas/src/main/java/com/baeldung/java8/entity/Human.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public void setAge(final int age) {
3737

3838
public static int compareByNameThenAge(final Human lhs, final Human rhs) {
3939
if (lhs.name.equals(rhs.name)) {
40-
return lhs.age - rhs.age;
40+
return Integer.compare(lhs.age, rhs.age);
4141
} else {
4242
return lhs.name.compareTo(rhs.name);
4343
}

core-java-modules/core-java-lambdas/src/test/java/com/baeldung/java8/Java8SortUnitTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public final void whenSortingEntitiesByNameThenAge_thenCorrectlySorted() {
5454
final List<Human> humans = Lists.newArrayList(new Human("Sarah", 12), new Human("Sarah", 10), new Human("Zack", 12));
5555
humans.sort((lhs, rhs) -> {
5656
if (lhs.getName().equals(rhs.getName())) {
57-
return lhs.getAge() - rhs.getAge();
57+
return Integer.compare(lhs.getAge(), rhs.getAge());
5858
} else {
5959
return lhs.getName().compareTo(rhs.getName());
6060
}

0 commit comments

Comments
 (0)