https://kotlinlang.org logo
Title
m

mike_shysh

03/07/2018, 8:37 AM
Hi, could you please help me to find the succinct way of sorting two arrays? currently I have
val referenceServiceFeaturesConflicts = refResponse.serviceFeaturesConflicts
.sortedWith(compareBy({ it.addonPrecedence }, { it.sfName }))
val actualServiceFeatureConflicts = response.serviceFeaturesConflicts
.sortedWith(compareBy({ it.addonPrecedence }, { it.sfName }))
a

Andreas Sinz

03/07/2018, 8:39 AM
why do you want to sort both arrays with a single line?
m

mike_shysh

03/07/2018, 8:40 AM
just to make code prettier)
s

spand

03/07/2018, 8:42 AM
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

mike_shysh

03/07/2018, 8:43 AM
val sortByNameAndPrecedence: List<ServiceFeaturesConflicts>.() -> List<ServiceFeaturesConflicts> = {
            this.sortedWith(compareBy({ it.addonPrecedence }, { it.sfName }))
        }
I made a lambda)
s

spand

03/07/2018, 8:45 AM
So its now 5 lines in total ?
n

nil2l

03/07/2018, 8:45 AM
Two arrays in single line: listOf(v1, v2).map { it.sortedWith(yourLmbda) } 🤖
m

mike_shysh

03/07/2018, 8:46 AM
I guesses there is some elegant solution=)
n

nil2l

03/07/2018, 8:47 AM
Also lambda may do sort here, not just compare.
m

mike_shysh

03/07/2018, 8:47 AM
@nil2l yeah, it's one of the possible ways
thanks