How to Sort List In A Custom Order In Kotlin?

8 minutes read

To sort a list in a custom order in Kotlin, you can create a custom Comparator that defines the order in which elements should be sorted. You can then use the sortedWith() function on the list, passing in your custom Comparator to sort the list according to the custom order specified. This allows you to sort the list based on your own criteria rather than the default sorting order.

Best Kotlin Books to Read in July 2024

1
Atomic Kotlin

Rating is 5 out of 5

Atomic Kotlin

2
Kotlin Cookbook: A Problem-Focused Approach

Rating is 4.9 out of 5

Kotlin Cookbook: A Problem-Focused Approach

3
Head First Kotlin: A Brain-Friendly Guide

Rating is 4.8 out of 5

Head First Kotlin: A Brain-Friendly Guide

4
Kotlin in Action

Rating is 4.7 out of 5

Kotlin in Action

5
Kotlin In-Depth: A Guide to a Multipurpose Programming Language for Server-Side, Front-End, Android, and Multiplatform Mobile (English Edition)

Rating is 4.6 out of 5

Kotlin In-Depth: A Guide to a Multipurpose Programming Language for Server-Side, Front-End, Android, and Multiplatform Mobile (English Edition)

6
Kotlin Design Patterns and Best Practices: Build scalable applications using traditional, reactive, and concurrent design patterns in Kotlin, 2nd Edition

Rating is 4.5 out of 5

Kotlin Design Patterns and Best Practices: Build scalable applications using traditional, reactive, and concurrent design patterns in Kotlin, 2nd Edition

7
Kotlin Programming: The Big Nerd Ranch Guide (Big Nerd Ranch Guides)

Rating is 4.4 out of 5

Kotlin Programming: The Big Nerd Ranch Guide (Big Nerd Ranch Guides)

8
Java to Kotlin

Rating is 4.2 out of 5

Java to Kotlin

9
Kotlin Essentials (Kotlin for Developers)

Rating is 4.1 out of 5

Kotlin Essentials (Kotlin for Developers)


How to customize the order of sorting in Kotlin?

In Kotlin, you can customize the order of sorting by using a Comparator. Here's an example of how you can customize the sorting order of a list of objects based on a specific property:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
data class Person(val name: String, val age: Int)

fun main() {
    val persons = listOf(
        Person("Alice", 30),
        Person("Bob", 25),
        Person("Charlie", 35)
    )

    val sortedPersons = persons.sortedWith(compareBy { it.age })

    sortedPersons.forEach { println(it) }
}


In this example, we are sorting a list of Person objects based on their age property. The sortedWith(compareBy { it.age }) function call customizes the sorting order by comparing the age property of each Person object.


You can also customize the sorting order using multiple properties or custom logic inside the compareBy block.

1
val sortedPersons = persons.sortedWith(compareBy({ it.age }, { it.name }))


This will first sort the list by age and then by name.


You can also write your custom Comparator by implementing the Comparator interface with a compare function.

1
2
3
4
5
6
7
8
9
val customComparator = Comparator { p1: Person, p2: Person ->
    when {
        p1.age > p2.age -> 1
        p1.age < p2.age -> -1
        else -> p1.name.compareTo(p2.name)
    }
}

val sortedPersons = persons.sortedWith(customComparator)


In this example, we define a custom Comparator that first compares the age property of the Person objects and then compares the name property if the ages are equal.


These are some ways you can customize the order of sorting in Kotlin using comparators.


What is the difference between sort() and sorted() in Kotlin?

In Kotlin, sort() is a function that sorts a mutable collection in-place, meaning it modifies the original collection. It does not return a new sorted collection, but instead sorts the elements within the existing collection.


On the other hand, sorted() is a function that creates a new sorted collection from an existing collection. It does not modify the original collection, but rather returns a new collection with the elements sorted according to the provided comparator.


In summary, sort() sorts the elements in-place, while sorted() creates a new sorted collection.


What is the default sorting order in Kotlin?

In Kotlin, when sorting elements using the sorted function or sortBy function, the default sorting order is ascending order. This means that elements will be sorted from smallest to largest based on their natural ordering or custom comparator provided.

Facebook Twitter LinkedIn Telegram Whatsapp Pocket

Related Posts:

In order to sort an array in Golang, you can follow these steps:Import the sort package in your Go code.Create an array that you want to sort.Use the sort.Sort function along with a custom sort.Interface implementation to sort the array.Here&#39;s an example o...
To sort a list in Haskell, you can use the sort function from the Data.List module. Here&#39;s how you can do it:Import the Data.List module by adding the following line at the top of your Haskell file: import Data.List Use the sort function to sort a list in ...
To sort a list in Groovy, you can use the sort() method on a list object. This method will sort the elements in the list in natural order. You can also use the sort method with a closure to define a custom sorting order. Another option is to use the sort metho...
In Kotlin, you can use the sortedBy function to sort a list of objects based on a specific property of the objects. For example, if you have a list of objects of type Person and you want to sort them based on their age, you can call sortedBy { it.age } on the ...
To sort an array in Java, you can use the Arrays.sort() method from the Java.util package. This method takes the array as input and sorts it in ascending order by default. You can also use the Collections.sort() method if you are working with a List instead of...
In Laravel, you can sort an array of objects using the sortBy method. This method allows you to specify a key to sort the objects by. For example, if you have an array of User objects and you want to sort them by their age, you can use the following code: $use...