diff --git a/projects/court-case-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/service/ConvictionService.kt b/projects/court-case-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/service/ConvictionService.kt index c545ff9bc0..c866c11ddd 100644 --- a/projects/court-case-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/service/ConvictionService.kt +++ b/projects/court-case-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/service/ConvictionService.kt @@ -72,12 +72,17 @@ class ConvictionService( } fun Event.toLatestOrSentencingCourtAppearanceOf(): CourtAppearanceBasic? { - return courtAppearances.filter { it.isSentenceing() } - .sortedByDescending(CourtAppearance::appearanceDate) - .maxByOrNull { it.appearanceDate } - ?.let { return it.toCourtAppearanceBasic() } - ?: courtAppearances.sortedByDescending(CourtAppearance::appearanceDate).maxByOrNull { it.appearanceDate } - ?.let { return it.toCourtAppearanceBasic() } + return courtAppearances + .stream() + .filter { it.isSentenceing() } + .max(Comparator.comparing(CourtAppearance::appearanceDate)) + .map { it.toCourtAppearanceBasic() }.orElseGet { + courtAppearances + .stream() + .max(Comparator.comparing(CourtAppearance::appearanceDate)) + .map { it.toCourtAppearanceBasic() } + .orElse(null) + } } fun CourtAppearance.toCourtAppearanceBasic(): CourtAppearanceBasic =