Sling Academy
Home/Kotlin/Sorting Lists in Kotlin: Natural and Custom Order

Sorting Lists in Kotlin: Natural and Custom Order

Last updated: December 05, 2024

Sorting is a fundamental operation in programming, and Kotlin, a statically typed programming language targeting the JVM, provides a multitude of ways to sort collections efficiently and elegantly. In this article, we'll explore the methods to sort lists in Kotlin both in natural order and with custom comparators.

Sorting Lists in Natural Order

Kotlin makes it straightforward to sort lists with its built-in functions. The sorted() function sorts a list in natural order. Consider this list of numbers:


val numbers = listOf(7, 5, 3, 8, 2)
val sortedNumbers = numbers.sorted()
println(sortedNumbers) // Output: [2, 3, 5, 7, 8]

The sorted() function is also applicable to a list of strings, which it sorts lexicographically (according to dictionary order):


val names = listOf("Charlie", "Bravo", "Alpha")
val sortedNames = names.sorted()
println(sortedNames) // Output: ["Alpha", "Bravo", "Charlie"]

Sorting Lists in Descending Order

Sometimes, you may need to sort a list in descending order. Kotlin provides the sortedDescending() function for this purpose:


val numbersDesc = numbers.sortedDescending()
println(numbersDesc) // Output: [8, 7, 5, 3, 2]

The equivalent for strings is:


val namesDesc = names.sortedDescending()
println(namesDesc) // Output: ["Charlie", "Bravo", "Alpha"]

Custom Sorting Using Comparators

In cases where natural sorting is not sufficient, Kotlin allows you to define custom comparators using the sortedBy() and sortedWith() functions.

Using sortedBy

The sortedBy() function sorts a list based on a specified property or field of the elements. For instance, if you have a list of data classes, you can sort the list based on a specific attribute:


data class Person(val name: String, val age: Int)

val people = listOf(
    Person("Alice", 29),
    Person("Bob", 25),
    Person("Charlie", 31)
)

val sortedByAge = people.sortedBy { it.age }
println(sortedByAge) // Output: [Person(name=Bob, age=25), Person(name=Alice, age=29), Person(name=Charlie, age=31)]

Using sortedWith

For more complex sorting logic, use the sortedWith() function, which requires a comparator. This is especially useful for sorting by multiple criteria:


val sortedByNameAndAge = people.sortedWith(compareBy({ it.name }, { it.age }))
println(sortedByNameAndAge) // Output: [Person(name=Alice, age=29), Person(name=Bob, age=25), Person(name=Charlie, age=31)]

The compareBy function can combine multiple selectors which the sortedWith function utilizes to determine order.

Extending the Comparator Logic

Kotlin also allows you to create custom comparator classes if you need fine-tuned control over the sorting mechanism.


class AgeComparator : Comparator<Person> {
    override fun compare(p1: Person, p2: Person): Int = p1.age.compareTo(p2.age)
}

val sortedByCustomComparator = people.sortedWith(AgeComparator())
println(sortedByCustomComparator) // Output: [Person(name=Bob, age=25), Person(name=Alice, age=29), Person(name=Charlie, age=31)]

Using a custom comparator gives you the ability to encapsulate complex sorting logic and reuse it throughout your application.

Conclusion

Sorting is an essential aspect of data manipulation in programming. With Kotlin's rich set of collection manipulation functions, managing and sorting lists is a streamlined task. Whether sorting in natural order or applying custom logic, Kotlin offers a concise and powerful approach.

Take advantage of Kotlin's expressive syntax and explore its sorting capabilities to enhance your applications by building efficient data handling processes.

Next Article: Filtering Lists with Conditions in Kotlin

Previous Article: Accessing Elements by Index in Lists in Kotlin

Series: Kotlin Collections

Kotlin

You May Also Like

  • How to Use Modulo for Cyclic Arithmetic in Kotlin
  • Kotlin: Infinite Loop Detected in Code
  • Fixing Kotlin Error: Index Out of Bounds in List Access
  • Setting Up JDBC in a Kotlin Application
  • Creating a File Explorer App with Kotlin
  • How to Work with APIs in Kotlin
  • What is the `when` Expression in Kotlin?
  • Writing a Script to Rename Multiple Files Programmatically in Kotlin
  • Using Safe Calls (`?.`) to Avoid NullPointerExceptions in Kotlin
  • Chaining Safe Calls for Complex Operations in Kotlin
  • Using the Elvis Operator for Default Values in Kotlin
  • Combining Safe Calls and the Elvis Operator in Kotlin
  • When to Avoid the Null Assertion Operator (`!!`) in Kotlin
  • How to Check for Null Values with `if` Statements in Kotlin
  • Using `let` with Nullable Variables for Scoped Operations in Kotlin
  • Kotlin: How to Handle Nulls in Function Parameters
  • Returning Nullable Values from Functions in Kotlin
  • Safely Accessing Properties of Nullable Objects in Kotlin
  • How to Use `is` for Nullable Type Checking in Kotlin