https://kotlinlang.org logo
Title
a

Ayden

09/24/2021, 2:21 PM
intervals = [[1,3],[2,6],[8,10],[15,18]]
Arrays.sort(intervals, compareBy { it[1] })
Hi guys, may i know how can i sort it in descending order based on the first index?
a

Alex Prince

09/24/2021, 2:26 PM
something like
val sorted = intervals.sortedDescendingBy{it.first()}
should do it
l

Luke

09/24/2021, 2:28 PM
sortByDescending
to sort the current array,
sortedByDescending
to generate a list without affecting the original array
a

Ayden

09/24/2021, 2:31 PM
Thank you.
j

Joffrey

09/24/2021, 2:38 PM
based on the first index
Note that
1
(as provided in the example) is not the first index, though. Using
it.first()
as suggested gives you index 0. Just keep this in mind when you choose what to pass to
sortedByDescending
.