Skip to content

Commit 4fd58b2

Browse files
committed
CrazyLambdas upgrade
* complete comparing and thenComparing method
1 parent 476c662 commit 4fd58b2

File tree

1 file changed

+8
-2
lines changed
  • 5-0-functional-programming/5-1-1-crazy-lambdas/src/main/java/com/bobocode/fp

1 file changed

+8
-2
lines changed

5-0-functional-programming/5-1-1-crazy-lambdas/src/main/java/com/bobocode/fp/CrazyLambdas.java

+8-2
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ public static BiFunction<Map<String, IntUnaryOperator>, String, IntUnaryOperator
265265
* @return a comparator instance
266266
*/
267267
public static <T, U extends Comparable<? super U>> Comparator<T> comparing(Function<? super T, ? extends U> mapper) {
268-
throw new ExerciseNotCompletedException();
268+
return (o1, o2) -> mapper.apply(o1).compareTo(mapper.apply(o2));
269269
}
270270

271271
/**
@@ -285,7 +285,13 @@ public static <T, U extends Comparable<? super U>> Comparator<T> comparing(Funct
285285
*/
286286
public static <T, U extends Comparable<? super U>> Comparator<T> thenComparing(
287287
Comparator<? super T> comparator, Function<? super T, ? extends U> mapper) {
288-
throw new ExerciseNotCompletedException();
288+
return (o1, o2) -> {
289+
var initialResult = comparator.compare(o1, o2);
290+
if (initialResult != 0) {
291+
return initialResult;
292+
}
293+
return mapper.apply(o1).compareTo(mapper.apply(o2));
294+
};
289295
}
290296

291297
/**

0 commit comments

Comments
 (0)