Kotlin Sorting by: sort(), sortBy(), sortWith() with Array, List Examples

https://loizenai.com/kotlin-sort-sortby-sortwith-with-array-list-examples/

In the tutorial, I will introduce how to sort Kotlin Array using sorting functions: sort(), sortBy() with selector function, and sortWith() with a comparator.

  • Sorts the array in-place according to the natural order of its elements.

Examples:

val simpleArray = arrayOf(1, 4, 2, 20, 3, 40, 23, 12, 51)

// fun <T : Comparable> Array.sort()
simpleArray.sort()
simpleArray.forEach { print("${it} ") }
/*
-> 1 2 3 4 12 20 23 40 51
*/

  • Kotlin List sort() examples

Method signature:

public fun <T : Comparable> MutableList.sort(): Unit
-> Sorts elements in the list in-place according to their natural sort order.

Practice:

package com.loizenai.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
*/

}

All at: https://loizenai.com/kotlin-sort-sortby-sortwith-with-array-list-examples/

#kotlin #sort

What is GEEK

Buddha Community

Kotlin Sorting by: sort(), sortBy(), sortWith() with Array, List Examples

Kotlin Sorting by: sort(), sortBy(), sortWith() with Array, List Examples - loizenai.com

https://loizenai.com/kotlin-sort-sortby-sortwith-with-array-list-examples/

In the tutorial, I will introduce how to sort Kotlin Array using sorting functions: sort(), sortBy() with selector function, and sortWith() with a comparator.

#kotlin #sorting #sortwith #sortby #array

Kotlin Array sort(), sortBy(), sortWith() » grokonez

https://grokonez.com/kotlin/kotlin-array-sort-sortby-sortwith

Kotlin Array sort(), sortBy(), sortWith()

In the tutorial, JavaSampleApproach will guide how to sort Kotlin Array by sort(), sortBy() with selector function, and sortWith() with a comparator.

I. Kotlin Array sort()

1. sort()

Method signature:
fun <T : Comparable<T>> Array<out T>.sort()

-> Sorts the array in-place according to the natural order of its elements.

Practice:

val simpleArray = arrayOf(1, 4, 2, 20, 3, 40, 23, 12, 51)

// fun <T : Comparable<T>> Array<out T>.sort()
simpleArray.sort()
simpleArray.forEach { print("${it} ") }
/*
	-> 1 2 3 4 12 20 23 40 51
*/

For descending sorting, we can use sortDescending(). Method signature:

public fun <T : Comparable<T>> Array<out T>.sortDescending(): Unit

Practice:

More at:

https://grokonez.com/kotlin/kotlin-array-sort-sortby-sortwith

Kotlin Array sort(), sortBy(), sortWith()

#kotlin #array #sort #sortby #sortwith

Kotlin Sorting by: sort(), sortBy(), sortWith() with Array, List Examples

https://loizenai.com/kotlin-sort-sortby-sortwith-with-array-list-examples/

In the tutorial, I will introduce how to sort Kotlin Array using sorting functions: sort(), sortBy() with selector function, and sortWith() with a comparator.

  • Sorts the array in-place according to the natural order of its elements.

Examples:

val simpleArray = arrayOf(1, 4, 2, 20, 3, 40, 23, 12, 51)

// fun <T : Comparable> Array.sort()
simpleArray.sort()
simpleArray.forEach { print("${it} ") }
/*
-> 1 2 3 4 12 20 23 40 51
*/

  • Kotlin List sort() examples

Method signature:

public fun <T : Comparable> MutableList.sort(): Unit
-> Sorts elements in the list in-place according to their natural sort order.

Practice:

package com.loizenai.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
*/

}

All at: https://loizenai.com/kotlin-sort-sortby-sortwith-with-array-list-examples/

#kotlin #sort

Kotlin – Sort List of Objects with Comparable » grokonez

https://grokonez.com/kotlin/kotlin-sort-list-objects-comparable

Kotlin – Sort List of Objects with Comparable

This tutorial shows you way to Sort List of Objects by Kotlin Comparable example.

Related posts:

I. Technology

- Java 1.8 - Kotlin 1.1.2

II. Overview

1. Goal

Sort list of three Date(year,month,day) objects.

2. Steps to do

- Implement Comparable interface for the class of objects you want to sort. - Override compareTo(other: T) method and: + return zero if this object is equal other + a negative number if it's less than other + a positive number if it's greater than other - Use sorted() method that returns a List.

III. Practice

1. Create Class for objects to be sorted

more at:

https://grokonez.com/kotlin/kotlin-sort-list-objects-comparable

Kotlin – Sort List of Objects with Comparable

#kotlin #sort #comparable #list

Kotlin - Sort List of Objects with Kotlin Comparator Example » grokonez

https://grokonez.com/kotlin/kotlin-sort-list-objects-kotlin-comparator-example

Kotlin – Sort List of Objects with Kotlin Comparator Example

This tutorial shows you way to Sort List of Objects by implementing Comparator example.

Related posts:

I. Technology

- Java 1.8 - Kotlin 1.1.2

II. Overview

1. Goal

Sort list of three MyDate(year,month,day) objects.
2. Steps to do
- Implement Comparator interface for the class that you use for handling sorting. - Override compare(object1: T, object2: T) method and: + return zero if object1 is equal object2 + a negative number if object1 is less than object2 + a positive number if object1 is greater than object2 - Use sortedWith(comparator: Comparator) method that returns a List.

III. Practice

1. Create Class for objects to be sorted


package com.javasampleapproach.objcomparator

data class MyDate (val year: Int, val month: Int, val day: Int) {

}

More at:

https://grokonez.com/kotlin/kotlin-sort-list-objects-kotlin-comparator-example

Kotlin – Sort List of Objects with Kotlin Comparator Example

#kotlin #sort #comparator