
- 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 - sortDescending() Function
The Kotlin array sortDescending() function is used to sort the elements of an array in place, arranging them in descending order according to their natural sort order.
This function also takes an optional parameterrangecontaining two values fromIndex, and toIndex.
The sort is stable. It means that equal elements preserve their order relative to each other after sorting.
Exception
Following are the exceptions −
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 sortDescending() function −
fun <T : Comparable<T>> Array<out T>.sortDescending()
Parameters
This function accepts following parameters −
fromIndex − it represent start of the range (inclusive) to sort.
toIndex − it represent end of the range (exclusive) to sort.
Return value
This function returns an array in sorting array.
Example 1
The following is a basic example to demonstrate the use of sortDescending() function −
fun main(args: Array<String>){ var arr = arrayOf<Int>(2, 4, 5, 6, 1) arr.sortDescending() println("descending array") for (i in 0 until arr.size){ println(arr.get(i)) } }
Output
The above code generate following output −
descending array 6 5 4 2 1
Example 2
Now here, we create an array of string. We then use sortDescending() function to sort the elements in descending order according to alphabet −
fun main(args: Array<String>){ var arr = arrayOf<String>("This", "is", "tutorialspoint", "India") arr.sortDescending() println("Descending order:") for (i in 0 until arr.size){ println(arr.get(i)) } }
Output
Following is the output −
Descending order: tutorialspoint is This India
Example 3
The below example uses sortDescending() function with specific range to modify some of array element in descending order −
fun main(args: Array<String>){ var arr = arrayOf<Char>('t', 'u', 't', 'o', 'r', 'i', 'a', 'l', 's', 'p', 'o', 'i', 'n', 't') // sort the array arr.sortDescending(3, 7) println("Descending order:") println(arr.joinToString()) }
Output
Following is the output −
Descending order: t, u, t, r, o, i, a, l, s, p, o, i, n, t