Sort a List of objects in Kotlin
This article explores different ways to sort a list of objects against multiple fields in Kotlin.
1. Using sortBy() function
The recommended solution to sort a list of objects is using the sortBy() function. The following example demonstrates this by sorting a list of Movie objects by their release year.
|
1 2 3 4 5 6 7 8 9 10 11 |
data class Movie(var name: String, var year: Int) fun main() { val movies = mutableListOf( Movie("Joker", 2019), Movie("Aquaman", 2018), Movie("Logan", 2017), Movie("Irishman", 2019) ) movies.sortBy { it.year } movies.forEach { println(it) } } |
Output:
Movie(name=Logan, year=2017)
Movie(name=Aquaman, year=2018)
Movie(name=Joker, year=2019)
Movie(name=Irishman, year=2019)
Note that the sortBy() function sorts the list only with a single field. If that field has the same value for two objects in the list, their relative ordering in the sorted list is not fixed. Therefore, it is preferred to compare objects using multiple fields.
2. Using sort() function
To sort a list of objects against the multiple fields, the class can implement the Comparable interface and override its abstract function compareTo() to define the position of an object relative to another object.
The following example demonstrates this by sorting a list of Movie objects using the sort() function, first by its release year and then by its name.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
data class Movie(var name: String, var year: Int) : Comparable<Movie> { override fun compareTo(other: Movie): Int { return if (year != other.year) year - other.year else name.compareTo(other.name) } } fun main() { val movies = mutableListOf( Movie("Joker", 2019), Movie("Aquaman", 2018), Movie("Logan", 2017), Movie("Irishman", 2019) ) movies.sort() movies.forEach { println(it) } } |
Output:
Movie(name=Logan, year=2017)
Movie(name=Aquaman, year=2018)
Movie(name=Irishman, year=2019)
Movie(name=Joker, year=2019)
3. Using sortWith() function
You can also use the sortWith() function to sort a list according to the order specified by the given comparator. The returned value decides the position of the first object relative to the second object.
The following example uses the Comparator object to compare two Movie objects, first by their release year and then by their name.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
data class Movie(var name: String, var year: Int) fun main() { val movies = mutableListOf( Movie("Joker", 2019), Movie("Aquaman", 2018), Movie("Logan", 2017), Movie("Irishman", 2019) ) movies.sortWith(Comparator { first, second -> if (first.year != second.year) { first.year - second.year } else { first.name.compareTo(second.name) } }) movies.forEach { println(it) } } |
Output:
Movie(name=Logan, year=2017)
Movie(name=Aquaman, year=2018)
Movie(name=Irishman, year=2019)
Movie(name=Joker, year=2019)
Alternatively, you can use the Comparator.thenComparing() function, which effectively combines multiple Comparators into one:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
data class Movie(var name: String, var year: Int) fun main() { val movies = mutableListOf( Movie("Joker", 2019), Movie("Aquaman", 2018), Movie("Logan", 2017), Movie("Irishman", 2019) ) val byYear = Comparator.comparing { movie: Movie -> movie.year } val byName = Comparator.comparing { movie: Movie -> movie.name } movies.sortWith(byYear.thenComparing(byName)) movies.forEach { println(it) } } |
Output:
Movie(name=Logan, year=2017)
Movie(name=Aquaman, year=2018)
Movie(name=Irishman, year=2019)
Movie(name=Joker, year=2019)
That’s all about sorting a list of objects in Kotlin.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)