https://loizenai.com Programming Tutorial
1602250678
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.
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
*/
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
https://loizenai.com Programming Tutorial
1603028531
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
https://loizenai.com Programming Tutorial
1619544644
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.
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
https://loizenai.com Programming Tutorial
1602250678
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.
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
*/
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
https://loizenai.com Programming Tutorial
1619547666
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:
Date(year,month,day)
objects.
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
.
more at:
https://grokonez.com/kotlin/kotlin-sort-list-objects-comparable
Kotlin – Sort List of Objects with Comparable
#kotlin #sort #comparable #list
https://loizenai.com Programming Tutorial
1618676327
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:
MyDate(year,month,day)
objects.
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
.
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