Kotlin List Sort: sort(), sortBy(), sortWith()

kotlin-list-sort-sortby-sortwith-feature-image

In the tutorial, Grokonez introduces how to sort a Kotlin List with methods: sort(), sortBy(), sortWith().

I. Kotlin List sort()

1. sort()

Method signature:

public fun > MutableList.sort(): Unit

-> Sorts elements in the list in-place according to their natural sort order.

Practice ->

package com.grokonez.kotlin

fun main(args : Array){
	val simpleList = mutableListOf(1, 4, 2, 20, 3, 40, 23, 12, 51)

	simpleList.sort();
	simpleList.forEach { print("${it} ") }
	/*
		-> 1 2 3 4 12 20 23 40 51
	*/
}

descending sorting -> we can use sortDescending(). Method signature:

public fun > MutableList.sortDescending(): Unit

-> Sorts elements in the list in-place descending according to their natural sort order.

Practice ->

package com.grokonez.kotlin

fun main(args : Array){
	val simpleList = mutableListOf(1, 4, 2, 20, 3, 40, 23, 12, 51)

	simpleList.sortDescending();
	simpleList.forEach { print("${it} ") }
	/*
		-> 51 40 23 20 12 4 3 2 1 
	*/
}
2. Sorted List Returned

If you want to return a List after sorting, we can use sorted() & sortedDescending() methods.
Method signature:

public fun > Iterable.sorted(): List
-> Returns a list of all elements sorted according to their natural sort order.

public fun > Iterable.sortedDescending(): List
-> Returns a list of all elements sorted descending according to their natural sort order.

II. Kotlin List sortBy() with Selector function

1. sortBy()

Method signature:

public inline fun > MutableList.sortBy(crossinline selector: (T) -> R?): Unit

-> Sorts elements in the list in-place according to natural sort order of the value returned by specified [selector] function.

Practice:

package com.grokonez.kotlin

data class Product(val name: String, val price: Double /*USD*/)

fun selector(p: Product): Double = p.price

fun main(args : Array){
	val products = mutableListOf(Product("iPhone 8 Plus 64G", 850.00),
							Product("iPhone 8 Plus 256G", 1100.00),
							Product("Apple iPod touch 16GB", 246.00),
							Product("Apple iPod Nano 16GB", 234.75),
							Product("iPad Pro 9.7-inch 32 GB", 474.98),
							Product("iPad Pro 9.7-inch 128G", 574.99),
							Product("Apple 42mm Smart Watch", 284.93))
		
	products.sortBy({selector(it)})
	
	products.forEach { println(it) }
	
	/*
		Product(name=Apple iPod Nano 16GB, price=234.75)
		Product(name=Apple iPod touch 16GB, price=246.0)
		Product(name=Apple 42mm Smart Watch, price=284.93)
		Product(name=iPad Pro 9.7-inch 32 GB, price=474.98)
		Product(name=iPad Pro 9.7-inch 128G, price=574.99)
		Product(name=iPhone 8 Plus 64G, price=850.0)
		Product(name=iPhone 8 Plus 256G, price=1100.0)
	 */
}

descending sorting, use the sort function: sortByDescending() ->

public inline fun > MutableList.sortByDescending(crossinline selector: (T) -> R?): Unit

-> Sorts elements in the list in-place descending according to natural sort order of the value returned by specified [selector] function.

Practice:

package com.grokonez.kotlin

data class Product(val name: String, val price: Double /*USD*/)

fun selector(p: Product): Double = p.price

fun main(args : Array){
	val products = mutableListOf(Product("iPhone 8 Plus 64G", 850.00),
							Product("iPhone 8 Plus 256G", 1100.00),
							Product("Apple iPod touch 16GB", 246.00),
							Product("Apple iPod Nano 16GB", 234.75),
							Product("iPad Pro 9.7-inch 32 GB", 474.98),
							Product("iPad Pro 9.7-inch 128G", 574.99),
							Product("Apple 42mm Smart Watch", 284.93))
		
	products.sortByDescending({selector(it)})
	
	products.forEach { println(it) }
	
	/*
		Product(name=iPhone 8 Plus 256G, price=1100.0)
		Product(name=iPhone 8 Plus 64G, price=850.0)
		Product(name=iPad Pro 9.7-inch 128G, price=574.99)
		Product(name=iPad Pro 9.7-inch 32 GB, price=474.98)
		Product(name=Apple 42mm Smart Watch, price=284.93)
		Product(name=Apple iPod touch 16GB, price=246.0)
		Product(name=Apple iPod Nano 16GB, price=234.75)
 	*/
}
2. Sorted List Returned

If we want to return a List after sorting, we can use sortedBy().

Method signature:

public inline fun > Iterable.sortedBy(crossinline selector: (T) -> R?): List
-> Returns a list of all elements sorted according to natural sort order of the value returned by specified [selector] function.

public inline fun > Iterable.sortedByDescending(crossinline selector: (T) -> R?): List
-> Returns a list of all elements sorted descending according to natural sort order of the value returned by specified [selector] function.

III. Kotlin List sortWith() with Comparator

1. sortWith()

Method signature:

public fun  MutableList.sortWith(comparator: Comparator): Unit

-> Sorts elements in the list in-place according to the order specified with [comparator].

Practice:

package com.grokonez.kotlin

data class Product(val name: String, val price: Double /*USD*/)

fun selector(p: Product): Double = p.price

fun main(args : Array){
	val products = mutableListOf(Product("iPhone 8 Plus 64G", 850.00),
							Product("iPhone 8 Plus 256G", 1100.00),
							Product("Apple iPod touch 16GB", 246.00),
							Product("Apple iPod Nano 16GB", 234.75),
							Product("iPad Pro 9.7-inch 32 GB", 474.98),
							Product("iPad Pro 9.7-inch 128G", 574.99),
							Product("Apple 42mm Smart Watch", 284.93))
		
	products.sortWith(object: Comparator{
								override fun compare(p1: Product, p2: Product): Int = when {
													p1.price > p2.price -> 1
													p1.price == p2.price -> 0
													else -> -1
												}
					  })
	
	products.forEach { println(it) }
	
	/*
		Product(name=Apple iPod Nano 16GB, price=234.75)
		Product(name=Apple iPod touch 16GB, price=246.0)
		Product(name=Apple 42mm Smart Watch, price=284.93)
		Product(name=iPad Pro 9.7-inch 32 GB, price=474.98)
		Product(name=iPad Pro 9.7-inch 128G, price=574.99)
		Product(name=iPhone 8 Plus 64G, price=850.0)
		Product(name=iPhone 8 Plus 256G, price=1100.0) 
	 */
}

Descending sorting -> we can re-implement comparator as below:

products.sortWith(object: Comparator{
							override fun compare(p1: Product, p2: Product): Int = when {
												p1.price < p2.price -> 1
												p1.price == p2.price -> 0
												else -> -1
											}
				  })
2. Sorted List Returned

If we want to return a List of all elements sorted according to the specified comparator, we can use sortedWith() method:

public fun  Iterable.sortedWith(comparator: Comparator): List

-> Returns a list of all elements sorted according to the specified [comparator].



By grokonez | February 1, 2019.

Last updated on February 6, 2020.



Related Posts


Got Something To Say:

Your email address will not be published. Required fields are marked *

*