4 minutes read

When we are working with collections, ordering the elements of a collection is a crucial task. Kotlin offers you an opportunity to define the order of collection elements in different ways: in natural, reverse, random, or custom order.

In this topic, we will present functions that facilitate the direct sorting based on the own criteria of the objects of a collection.

Sorting objects

In collections, natural order implies arranging numbers in ascending order, strings in alphabetical order, and dates in timeline order. Sorting in descending order is also possible.

In Kotlin, we have two functions: sorted() and sortedDescending() for sorting the elements of a collection in natural order as defined in the Comparable interface. sorted() gives us a collection where the elements are sorted in ascending order. sortedDescending() is used to apply descending order. These methods are available in all basic Kotlin collections.

fun main() {
    val numbers = listOf(3, 5, 6, 4, 1, 8, 2, 7)
    println(numbers.sorted()) // [1, 2, 3, 4, 5, 6, 7, 8]
    println(numbers.sortedDescending()) // [8, 7, 6, 5, 4, 3, 2, 1]

    val words = listOf("racecar", "mom", "dad", "abracadabra", "MANDRAKE")
    println(words.sorted()) // [MANDRAKE, abracadabra, dad, mom, racecar]
    println(words.sortedDescending()) // [racecar, mom, dad, abracadabra, MANDRAKE]
}

Reverse order

We can obtain a collection in reverse order (based on the index positions) using two functions: reversed() and asReversed(). Let's take a look at the differences between them. Remember in ASCII or UTF-8, the uppercase letters are assigned lower numeric codes than their corresponding lowercase letters. So, the uppercase letters come before the lowercase letters when ordered by their decimal codes.

The reversed() function returns a copy of the original collection in reverse index order. If you change the original collection, the changes won't affect the copy.

fun main() {
    val numbers = mutableListOf(3, 5, 6, 4, 1, 8, 2, 7)
    val reversedNumbers = numbers.reversed()
    
    println(numbers) // [3, 5, 6, 4, 1, 8, 2, 7]
    println(reversedNumbers) // [7, 2, 8, 1, 4, 6, 5, 3]
    numbers.add(9)
    println(numbers) // [3, 5, 6, 4, 1, 8, 2, 7, 9]
    println(reversedNumbers) // [7, 2, 8, 1, 4, 6, 5, 3]

    val words = listOf("racecar", "mom", "dad", "abracadabra", "MANDRAKE")
    val wordsReversed = words.reversed()
    
    println(words) // [racecar, mom, dad, abracadabra, MANDRAKE]
    println(wordsReversed) // [MANDRAKE, abracadabra, dad, mom, racecar]
}

The asReversed() method returns a reference to the original collection in reverse index order. It is lighter because it does not create a new copy, but if the original collection changes, the changes are reflected in the reversed list. We must be careful if we work with mutable collections.

fun main() {
    val numbers = mutableListOf(3, 5, 6, 4, 1, 8, 2, 7)
    val numbersAsReversed = numbers.asReversed()
    
    println(numbers) // [3, 5, 6, 4, 1, 8, 2, 7]
    println(numbersAsReversed) // [7, 2, 8, 1, 4, 6, 5, 3]
    
    numbers.add(9)
    
    println(numbers) // [3, 5, 6, 4, 1, 8, 2, 7, 9]
    println(numbersAsReversed) // [9, 7, 2, 8, 1, 4, 6, 5, 3]

    val words = listOf("racecar", "mom", "dad", "abracadabra", "MANDRAKE")
    val wordsAsReversed = words.asReversed()
    println(words) // [racecar, mom, dad, abracadabra, MANDRAKE]
    println(wordsAsReversed) // [MANDRAKE, abracadabra, dad, mom, racecar]
}

Random order

The shuffled() method gives you a new collection where the original items are randomly shuffled. You can use it without arguments or with a Random instance as the source of randomness. Using an integer as the seed in the Random generator will shuffle the elements in a random way, which can be reproduced if you keep using the same seed.

fun main() {
    val numbers = mutableListOf(3, 5, 6, 4, 1, 8, 2, 7)
    val shuffledNumbers = numbers.shuffled()

    println(numbers) // [3, 5, 6, 4, 1, 8, 2, 7]
    println(shuffledNumbers) // [5, 3, 6, 7, 1, 8, 2, 4]
    
    val words = listOf("racecar", "mom", "dad", "abracadabra", "MANDRAKE")
    val shuffledWords = words.shuffled(Random(1))

    println(words) // [racecar, mom, dad, abracadabra, MANDRAKE]
    println(shuffledWords) // [MANDRAKE, abracadabra, mom, dad, racecar]
}

Conclusion

In this topic, we have seen different ways to arrange the elements of a collection – in natural order, reverse order, and random order. They offer us an elegant and easy way of organizing our collections.

Now is the time to check what you have learned about ordering. Let's go for it!

76 learners liked this piece of theory. 2 didn't like it. What about you?
Report a typo