Hi, could you please help me to find the succinct ...
# announcements
m
Hi, could you please help me to find the succinct way of sorting two arrays? currently I have
Copy code
val referenceServiceFeaturesConflicts = refResponse.serviceFeaturesConflicts
.sortedWith(compareBy({ it.addonPrecedence }, { it.sfName }))
val actualServiceFeatureConflicts = response.serviceFeaturesConflicts
.sortedWith(compareBy({ it.addonPrecedence }, { it.sfName }))
a
why do you want to sort both arrays with a single line?
m
just to make code prettier)
s
I expect the most readable code would be to extract the comparator to a variable if thats possible
👍 1
Otherwise it seems pretty fine to me
m
Copy code
val sortByNameAndPrecedence: List<ServiceFeaturesConflicts>.() -> List<ServiceFeaturesConflicts> = {
            this.sortedWith(compareBy({ it.addonPrecedence }, { it.sfName }))
        }
I made a lambda)
s
So its now 5 lines in total ?
n
Two arrays in single line: listOf(v1, v2).map { it.sortedWith(yourLmbda) } 🤖
m
I guesses there is some elegant solution=)
n
Also lambda may do sort here, not just compare.
m
@nil2l yeah, it's one of the possible ways
thanks