Kotlin Tip #39: Use sortBy and sortedBy for Collection Sorting — 100 Kotlin Tips in 100 Days

Raphael De Lio
Kotlin with Raphael De Lio
3 min readMar 22, 2024

--

Twitter | LinkedIn | YouTube | Instagram
Tip #38: Use groupBy to Organize Collections by Criteria

When working with collections in Kotlin, organizing elements into a specific order is a common task. Whether it’s sorting a list of names alphabetically or arranging objects by a particular property, Kotlin provides functions to accomplish this: sortBy and sortedBy. These functions offer a clear, concise way to sort collections, improving code readability and efficiency.

Both sortBy and sortedBy are extension functions that Kotlin offers for sorting collections. While they serve similar purposes, their applications slightly differ based on the context and the type of collection you're dealing with.

  • sortBy is used with mutable collections. It sorts the collection in-place, modifying the original collection.
  • sortedBy, on the other hand, is used with both mutable and immutable collections. It returns a new list with the elements sorted, leaving the original collection unchanged.

Let’s dive into some examples to see how these functions can be applied to real-world scenarios.

Sorting a List of Strings

Suppose you have a list of names that you want to sort alphabetically:

In this example, sortedBy takes a selector function { it } that simply returns each element, and the list is sorted based on the natural order of the strings.

Sorting Objects by a Property

Consider a list of User objects where each User has a name and an age. If you want to sort the users by their age, you can do so easily with sortedBy:

The sortedBy function here takes a selector function { it.age }, which extracts the age property from each User object to determine the order.

In-place Sorting with sortBy

If you’re working with a mutable list and want to sort it directly, sortBy can be used:

This time, the original users list is sorted in-place, and no new list is created.

Remember, clear and efficient sorting not only aids in data organization but also enhances the overall readability and maintainability of your code. With sortBy and sortedBy, Kotlin offers a straightforward approach to achieving this, keeping your collections orderly and your codebase clean.

I hope you have enjoyed this tip of our series! Don’t forget to subscribe and stay tuned for more Kotlin tips!

Stay curious!

Tip #40: Explore the distinct and distinctBy for removing duplicates from collections

Contribute

Writing takes time and effort. I love writing and sharing knowledge, but I also have bills to pay. If you like my work, please, consider donating through Buy Me a Coffee: https://www.buymeacoffee.com/RaphaelDeLio

Or by sending me BitCoin: 1HjG7pmghg3Z8RATH4aiUWr156BGafJ6Zw

Follow Me on Social Media

Stay connected and dive deeper into the world of Kotlin with me! Follow my journey across all major social platforms for exclusive content, tips, and discussions.

Twitter | LinkedIn | YouTube | Instagram

--

--