In this tutorial, I will show you many ways to sort Kotlin List using sort
, sorted
, sortBy
, sortedBy
, sortWith
, sortedWith
methods.
Related Posts:
– Kotlin List & Mutable List tutorial with examples
– Kotlin – Sort List of custom Objects
Contents
Kotlin sort
You can use sort() method to sort a Mutable List in-place, and sortDescending() for descending order.
Ascending
val nums = mutableListOf(3, 1, 7, 2, 8, 6)
nums.sort()
// nums: [1, 2, 3, 6, 7, 8]
Descending
nums.sortDescending();
// nums: [8, 7, 6, 3, 2, 1]
Kotlin sorted
sorted() and sortedDescending() don’t change the original List. Instead, they return another sorted List.
Ascending
val nums = mutableListOf(3, 1, 7, 2, 8, 6)
val sortedNums = nums.sorted()
// nums: [3, 1, 7, 2, 8, 6]
// sortedNums: [1, 2, 3, 6, 7, 8]
Descending
val sortedNumsDescending = nums.sortedDescending()
// nums: [3, 1, 7, 2, 8, 6]
// sortedNumsDescending: [8, 7, 6, 3, 2, 1]
Kotlin sortBy
sortBy() helps us to sort a Multable List in-place by specific field. We need to pass a selector function as an argument.
For descending order, we use sortByDescending().
Ascending
val myDates = mutableListOf(
MyDate(4, 3),
MyDate(5, 16),
MyDate(1, 29)
)
myDates.sortBy { it.month }
myDates.forEach { println(it) }
/*
MyDate(month=1, day=29)
MyDate(month=4, day=3)
MyDate(month=5, day=16)
*/
Descending
myDates.sortByDescending { it.month }
myDates.forEach { println(it) }
/*
MyDate(month=5, day=16)
MyDate(month=4, day=3)
MyDate(month=1, day=29)
*/
Kotlin sortedBy
Instead of changing the order of original List. sortedBy() and sortedByDescending() return a sorted List, the original List isn’t affected.
Ascending
val myDates = mutableListOf(
MyDate(4, 3),
MyDate(5, 16),
MyDate(1, 29)
)
val sortedDates = myDates.sortedBy { it.month }
myDates.forEach { println(it) }
/*
MyDate(month=4, day=3)
MyDate(month=5, day=16)
MyDate(month=1, day=29)
*/
sortedDates.forEach { println(it) }
/*
MyDate(month=1, day=29)
MyDate(month=4, day=3)
MyDate(month=5, day=16)
*/
Descending
val sortedDatesDescending = myDates.sortedByDescending { it.month }
myDates.forEach { println(it) }
/*
MyDate(month=4, day=3)
MyDate(month=5, day=16)
MyDate(month=1, day=29)
*/
sortedDatesDescending.forEach { println(it) }
/*
MyDate(month=5, day=16)
MyDate(month=4, day=3)
MyDate(month=1, day=29)
*/
Kotlin sortWith
How about continue to sort day after sorting month?
We’re gonna use sortWith() for ascending order and additional method reverse() for descending order.
Ascending
val myDates = mutableListOf(
MyDate(8, 19),
MyDate(5, 16),
MyDate(1, 29),
MyDate(5, 10),
MyDate(8, 3)
)
myDates.sortWith(compareBy { it.month }.thenBy { it.day })
myDates.forEach { println(it) }
/*
MyDate(month=1, day=29)
MyDate(month=5, day=10)
MyDate(month=5, day=16)
MyDate(month=8, day=3)
MyDate(month=8, day=19)
*/
Descending
myDates.reverse()
sortedDatesDescending.forEach { println(it) }
/*
MyDate(month=8, day=19)
MyDate(month=8, day=3)
MyDate(month=5, day=16)
MyDate(month=5, day=10)
MyDate(month=1, day=29)
*/
Kotlin sortedWith
sortedWith() and reversed() return a sorted List instead of changing original List order.
Ascending
val sortedDates = myDates.sortedWith(compareBy { it.month }.thenBy { it.day })
myDates.forEach { println(it) }
/*
MyDate(month=8, day=19)
MyDate(month=5, day=16)
MyDate(month=1, day=29)
MyDate(month=5, day=10)
MyDate(month=8, day=3)
*/
sortedDates.forEach { println(it) }
/*
MyDate(month=1, day=29)
MyDate(month=5, day=10)
MyDate(month=5, day=16)
MyDate(month=8, day=3)
MyDate(month=8, day=19)
*/
Descending
val sortedDatesDescending = myDates.sortedWith(compareBy { it.month }.thenBy { it.day }).reversed()
myDates.forEach { println(it) }
/*
MyDate(month=8, day=19)
MyDate(month=5, day=16)
MyDate(month=1, day=29)
MyDate(month=5, day=10)
MyDate(month=8, day=3)
*/
sortedDatesDescending.forEach { println(it) }
/*
MyDate(month=8, day=19)
MyDate(month=8, day=3)
MyDate(month=5, day=16)
MyDate(month=5, day=10)
MyDate(month=1, day=29)
*/
Sort List of Objects
It’s more complicated, so I write in a separated Tutorial. You can find at:
Kotlin – Sort List of custom Objects
Thanks!
Your tutorial is very well written, much better than books on the market.
Every weekend I used to visit your articles, your Kotlin tutorials really help me a lot.