- Kotlin - Home
- Kotlin - Overview
- Kotlin - Environment Setup
- Kotlin - Architecture
- Kotlin - Basic Syntax
- Kotlin - Comments
- Kotlin - Keywords
- Kotlin - Variables
- Kotlin - Data Types
- Kotlin - Operators
- Kotlin - Booleans
- Kotlin - Strings
- Kotlin - Arrays
- Kotlin - Ranges
- Kotlin - Functions
- Kotlin Control Flow
- Kotlin - Control Flow
- Kotlin - if...Else Expression
- Kotlin - When Expression
- Kotlin - For Loop
- Kotlin - While Loop
- Kotlin - Break and Continue
- Kotlin Collections
- Kotlin - Collections
- Kotlin - Lists
- Kotlin - Sets
- Kotlin - Maps
- Kotlin Objects and Classes
- Kotlin - Class and Objects
- Kotlin - Constructors
- Kotlin - Inheritance
- Kotlin - Abstract Classes
- Kotlin - Interface
- Kotlin - Visibility Control
- Kotlin - Extension
- Kotlin - Data Classes
- Kotlin - Sealed Class
- Kotlin - Generics
- Kotlin - Delegation
- Kotlin - Destructuring Declarations
- Kotlin - Exception Handling
Kotlin Array - sortWith() Function
The Kotlin array sortWith() function is used to sort the element of an array in place according to the order specified by the comparator function.
This function also accepts two optional parameters: formIndex and toIndex, to sort the elements in the range according to the comparator.
A comparator is a function that returns 1, -1, or 0 depending on the comparison of the two variables passed to the function.
Exception
Following are the exception −
IndexOutOfBoundsException − If fromIndex is less than zero or toIndex is greater than the size of this array
IllegalArgumentException − If fromIndex is greater than toIndex.
Syntax
Following is the syntax of Kotlin array sortWith() function −
fun <T> Array<T>.sortWith(comparator: Comparator<in T>)
Parameters
This function accepts following parameters.
formInfex − It represents start of the range (inclusive) to sort, 0 by default.
toIndex − It represents end of the range (exclusive) to sort, size of this array by default.
Return value
This function does not returns any value. It modify the original array into sorted array.
Example 1
Following is a basic example to demonstrate the use of sortWith() function −
fun main(args: Array<String>){
val arr = arrayOf<Int>(5, 3, 6, 7, 4)
arr.sortWith(Comparator<Int>{ a, b ->
when {
a > b -> 1
a < b -> -1
else -> 0
}
})
print("Sorted array: ")
println(arr.joinToString())
}
Output
The above code generate following output −
Sorted array: 3, 4, 5, 6, 7
Example 2
Now, we create an array of string. We then use sortWith() function to sort the elements of an array in descending order −
fun main(args: Array<String>){
val arr = arrayOf<String>("Daisy", "Rahul", "Aman", "Prajwalani")
arr.sortWith(Comparator<String>{ a, b ->
when {
a < b -> 1
a > b -> -1
else -> 0
}
})
println("Sorted array: " + arr.joinToString())
}
Output
Following is the output −
Sorted array: Rahul, Prajwalani, Daisy, Aman
Example 3
The below example creates a comparator to sort elements in descending order. We then use sortWith() function to sort elements of an array according to this comparator −
fun main(args: Array<String>) {
val numbers = arrayOf(42, 10, 23, 89, 5, 34)
// Display the original array
println("Original array: ${numbers.joinToString(", ")}")
// create a custom comparator to sort the array in descending order
val descendingComparator = Comparator<Int> { a, b -> when {
a < b -> 1
a > b -> -1
else -> 0 }
}
// Sort the array using the custom comparator
numbers.sortWith(descendingComparator)
// Display the sorted array
println("Sorted array with comparator:"+ numbers.joinToString())
}
Output
The above code produce following output −
Original array: 42, 10, 23, 89, 5, 34 Sorted array with comparator:89, 42, 34, 23, 10, 5