Kotlin Sort Object List with Kotlin Selector function – crossinline selector: (T) -> R?

kotlin-sort-object-list-kotlin-selector-function-crossinline-selector-t-r-feature-image

In the tutorial, JavaSampleApproach will show you how to Sort Kotlin Object List with Kotlin Selector function – crossinline selector: (T) -> R?

Related posts:
Sort List of Objects with Kotlin Comparator Example
Sort List of Objects with Kotlin Comparable
Kotlin Binary Search for Object List with Comparison function – comparison: (T) -> Int
Kotlin Binary Search for Object List with Selector function – crossinline selector: (T) -> K?

I. Technologies

– Java 1.8
– Kotlin 1.1.61

II. Kotlin Sort Object List with Selector

We have sortedBy(crossinline selector: (T) -> R?) for ascending sorted list, and sortedByDescending(crossinline selector: (T) -> R?) for descending sorted list.

– Method signature of sortedBy(crossinline selector: (T) -> R?)


public inline fun > Iterable.sortedBy(crossinline selector: (T) -> R?): List

– Method signature of sortedByDescending(crossinline selector: (T) -> R?)


public inline fun > Iterable.sortedByDescending(crossinline selector: (T) -> R?): List

So We need define an appropriate selector function for them.

-> Step to do:

  • Create data model
  • Create selector function
  • Do Sorting

1. Create data model

– Create Product model:


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

2. Create selector function


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

3. Do Sorting


val sortedProducts = products.sortedBy({selector(it)})
					// -> simple representation solution
					// val sortedProducts = products.sortedBy{it.price}

val descendingSortedProducts = products.sortedByDescending({selector(it)})
					// -> simple representation solution
					// val descendingSortedProducts = products.sortedByDescending{it.price}

III. Full Sourcecode


package com.javasampleapproach.sortwithselector

// define Product data model
data class Product(val name: String? = null, val price: Double /*USD*/)

// define selector function for sorting
fun selector(p: Product): Double = p.price

fun main(args : Array){
	val products = listOf(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))
	
	// Use sortedBy() method signature as below:
	// -> public inline fun > Iterable.sortedBy(crossinline selector: (T) -> R?): List
	val sortedProducts = products.sortedBy({selector(it)})
						// -> simple representation solution
						// val sortedProducts = products.sortedBy{it.price}
	println("==================Sorted List==================")
	sortedProducts.forEach{println(it)}
	// print on console an ascending product list:
	// ->
	/*
		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)
	*/
	
	val descendingSortedProducts = products.sortedByDescending({selector(it)})
						// -> simple representation solution
						// val sortedProducts = products.sortedByDescending{it.price}
	println("\n==================Descending Sorted List==================")
	descendingSortedProducts.forEach{println(it)}
	// print on console an descending product list:
	// ->
	/*
		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) 	
	 */
}


By grokonez | December 15, 2017.

Last updated on April 17, 2021.



Related Posts


Got Something To Say:

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

*