sort

Sorts the array in-place.

Since Kotlin

1.3

Samples

import kotlin.test.*

fun main() { 
   //sampleStart 
   val intArray = intArrayOf(4, 3, 2, 1)

// before sorting
println(intArray.joinToString()) // 4, 3, 2, 1

intArray.sort()

// after sorting
println(intArray.joinToString()) // 1, 2, 3, 4 
   //sampleEnd
}

fun UIntArray.sort(fromIndex: Int = 0, toIndex: Int = size)(source)
fun ULongArray.sort(fromIndex: Int = 0, toIndex: Int = size)(source)
fun UByteArray.sort(fromIndex: Int = 0, toIndex: Int = size)(source)
fun UShortArray.sort(fromIndex: Int = 0, toIndex: Int = size)(source)
expect fun ByteArray.sort(fromIndex: Int = 0, toIndex: Int = size)(source)
expect fun ShortArray.sort(fromIndex: Int = 0, toIndex: Int = size)(source)
expect fun IntArray.sort(fromIndex: Int = 0, toIndex: Int = size)(source)
expect fun LongArray.sort(fromIndex: Int = 0, toIndex: Int = size)(source)
expect fun FloatArray.sort(fromIndex: Int = 0, toIndex: Int = size)(source)
expect fun DoubleArray.sort(fromIndex: Int = 0, toIndex: Int = size)(source)
expect fun CharArray.sort(fromIndex: Int = 0, toIndex: Int = size)(source)

Sorts a range in the array in-place.

Since Kotlin

1.4

Parameters

fromIndex

the start of the range (inclusive) to sort, 0 by default.

toIndex

the end of the range (exclusive) to sort, size of this array by default.

Throws

if fromIndex is less than zero or toIndex is greater than the size of this array.

Samples

import kotlin.test.*

fun main() { 
   //sampleStart 
   val intArray = intArrayOf(4, 3, 2, 1)

// before sorting
println(intArray.joinToString()) // 4, 3, 2, 1

intArray.sort(0, 3)

// after sorting
println(intArray.joinToString()) // 2, 3, 4, 1 
   //sampleEnd
}

expect fun IntArray.sort()(source)
expect fun LongArray.sort()(source)
expect fun ByteArray.sort()(source)
expect fun ShortArray.sort()(source)
expect fun DoubleArray.sort()(source)
expect fun FloatArray.sort()(source)
expect fun CharArray.sort()(source)

Sorts the array in-place.

Since Kotlin

1.0

Samples

import kotlin.test.*

fun main() { 
   //sampleStart 
   val intArray = intArrayOf(4, 3, 2, 1)

// before sorting
println(intArray.joinToString()) // 4, 3, 2, 1

intArray.sort()

// after sorting
println(intArray.joinToString()) // 1, 2, 3, 4 
   //sampleEnd
}

expect fun <T : Comparable<T>> Array<out T>.sort()(source)

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

The sort is stable. It means that equal elements preserve their order relative to each other after sorting.

Since Kotlin

1.0

Samples

import kotlin.test.*

fun main() { 
   //sampleStart 
   class Person(val firstName: String, val lastName: String) : Comparable<Person> {
    override fun compareTo(other: Person): Int = this.lastName.compareTo(other.lastName)
    override fun toString(): String = "$firstName $lastName"
}

val people = arrayOf(
    Person("Ragnar", "Lodbrok"),
    Person("Bjorn", "Ironside"),
    Person("Sweyn", "Forkbeard")
)

// before sorting
println(people.joinToString()) // Ragnar Lodbrok, Bjorn Ironside, Sweyn Forkbeard

people.sort()

// after sorting
println(people.joinToString()) // Sweyn Forkbeard, Bjorn Ironside, Ragnar Lodbrok 
   //sampleEnd
}

expect fun <T : Comparable<T>> Array<out T>.sort(fromIndex: Int = 0, toIndex: Int = size)(source)

Sorts a range in the array in-place.

The sort is stable. It means that equal elements preserve their order relative to each other after sorting.

Since Kotlin

1.4

Parameters

fromIndex

the start of the range (inclusive) to sort, 0 by default.

toIndex

the end of the range (exclusive) to sort, size of this array by default.

Throws

if fromIndex is less than zero or toIndex is greater than the size of this array.

Samples

import kotlin.test.*

fun main() { 
   //sampleStart 
   class Person(val firstName: String, val lastName: String) : Comparable<Person> {
    override fun compareTo(other: Person): Int = this.lastName.compareTo(other.lastName)
    override fun toString(): String = "$firstName $lastName"
}

val people = arrayOf(
    Person("Ragnar", "Lodbrok"),
    Person("Bjorn", "Ironside"),
    Person("Sweyn", "Forkbeard")
)

// before sorting
println(people.joinToString()) // Ragnar Lodbrok, Bjorn Ironside, Sweyn Forkbeard

people.sort(0, 2)

// after sorting
println(people.joinToString()) // Bjorn Ironside, Ragnar Lodbrok, Sweyn Forkbeard 
   //sampleEnd
}

expect fun <T : Comparable<T>> MutableList<T>.sort()(source)

Sorts elements in the list in-place according to their natural sort order.

The sort is stable. It means that equal elements preserve their order relative to each other after sorting.

Since Kotlin

1.0

Samples

import kotlin.test.*

fun main() { 
   //sampleStart 
   val mutableList = mutableListOf(4, 3, 2, 1)

// before sorting
println(mutableList.joinToString()) // 4, 3, 2, 1

mutableList.sort()

// after sorting
println(mutableList.joinToString()) // 1, 2, 3, 4 
   //sampleEnd
}
actual fun IntArray.sort()(source)
actual fun LongArray.sort()(source)
actual fun ByteArray.sort()(source)
actual fun ShortArray.sort()(source)
actual fun DoubleArray.sort()(source)
actual fun FloatArray.sort()(source)
actual fun CharArray.sort()(source)

Sorts the array in-place.

Since Kotlin

1.1

Samples

import kotlin.test.*

fun main() { 
   //sampleStart 
   val intArray = intArrayOf(4, 3, 2, 1)

// before sorting
println(intArray.joinToString()) // 4, 3, 2, 1

intArray.sort()

// after sorting
println(intArray.joinToString()) // 1, 2, 3, 4 
   //sampleEnd
}

actual fun <T : Comparable<T>> Array<out T>.sort()(source)

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

The sort is stable. It means that equal elements preserve their order relative to each other after sorting.

Since Kotlin

1.1

Samples

import kotlin.test.*

fun main() { 
   //sampleStart 
   class Person(val firstName: String, val lastName: String) : Comparable<Person> {
    override fun compareTo(other: Person): Int = this.lastName.compareTo(other.lastName)
    override fun toString(): String = "$firstName $lastName"
}

val people = arrayOf(
    Person("Ragnar", "Lodbrok"),
    Person("Bjorn", "Ironside"),
    Person("Sweyn", "Forkbeard")
)

// before sorting
println(people.joinToString()) // Ragnar Lodbrok, Bjorn Ironside, Sweyn Forkbeard

people.sort()

// after sorting
println(people.joinToString()) // Sweyn Forkbeard, Bjorn Ironside, Ragnar Lodbrok 
   //sampleEnd
}

actual fun <T : Comparable<T>> Array<out T>.sort(fromIndex: Int = 0, toIndex: Int = size)(source)

Sorts a range in the array in-place.

The sort is stable. It means that equal elements preserve their order relative to each other after sorting.

Since Kotlin

1.4

Parameters

fromIndex

the start of the range (inclusive) to sort, 0 by default.

toIndex

the end of the range (exclusive) to sort, size of this array by default.

Throws

if fromIndex is less than zero or toIndex is greater than the size of this array.

Samples

import kotlin.test.*

fun main() { 
   //sampleStart 
   class Person(val firstName: String, val lastName: String) : Comparable<Person> {
    override fun compareTo(other: Person): Int = this.lastName.compareTo(other.lastName)
    override fun toString(): String = "$firstName $lastName"
}

val people = arrayOf(
    Person("Ragnar", "Lodbrok"),
    Person("Bjorn", "Ironside"),
    Person("Sweyn", "Forkbeard")
)

// before sorting
println(people.joinToString()) // Ragnar Lodbrok, Bjorn Ironside, Sweyn Forkbeard

people.sort(0, 2)

// after sorting
println(people.joinToString()) // Bjorn Ironside, Ragnar Lodbrok, Sweyn Forkbeard 
   //sampleEnd
}

actual fun ByteArray.sort(fromIndex: Int = 0, toIndex: Int = size)(source)
actual fun ShortArray.sort(fromIndex: Int = 0, toIndex: Int = size)(source)
actual fun IntArray.sort(fromIndex: Int = 0, toIndex: Int = size)(source)
actual fun LongArray.sort(fromIndex: Int = 0, toIndex: Int = size)(source)
actual fun FloatArray.sort(fromIndex: Int = 0, toIndex: Int = size)(source)
actual fun DoubleArray.sort(fromIndex: Int = 0, toIndex: Int = size)(source)
actual fun CharArray.sort(fromIndex: Int = 0, toIndex: Int = size)(source)

Sorts a range in the array in-place.

Since Kotlin

1.4

Parameters

fromIndex

the start of the range (inclusive) to sort, 0 by default.

toIndex

the end of the range (exclusive) to sort, size of this array by default.

Throws

if fromIndex is less than zero or toIndex is greater than the size of this array.

Samples

import kotlin.test.*

fun main() { 
   //sampleStart 
   val intArray = intArrayOf(4, 3, 2, 1)

// before sorting
println(intArray.joinToString()) // 4, 3, 2, 1

intArray.sort(0, 3)

// after sorting
println(intArray.joinToString()) // 2, 3, 4, 1 
   //sampleEnd
}

actual fun <T : Comparable<T>> MutableList<T>.sort()(source)

Sorts elements in the list in-place according to their natural sort order.

The sort is stable. It means that equal elements preserve their order relative to each other after sorting.

Since Kotlin

1.1

Samples

import kotlin.test.*

fun main() { 
   //sampleStart 
   val mutableList = mutableListOf(4, 3, 2, 1)

// before sorting
println(mutableList.joinToString()) // 4, 3, 2, 1

mutableList.sort()

// after sorting
println(mutableList.joinToString()) // 1, 2, 3, 4 
   //sampleEnd
}

fun <T> Array<out T>.sort(comparison: (a: T, b: T) -> Int)(source)

Deprecated

Warning since 1.6

Use sortWith instead

Replace with

this.sortWith(Comparator(comparison))

Sorts the array in-place according to the order specified by the given comparison function.

The sort is stable. It means that equal elements preserve their order relative to each other after sorting.

Since Kotlin

1.1

inline fun ByteArray.sort(noinline comparison: (a: Byte, b: Byte) -> Int)(source)
inline fun ShortArray.sort(noinline comparison: (a: Short, b: Short) -> Int)(source)
inline fun IntArray.sort(noinline comparison: (a: Int, b: Int) -> Int)(source)
inline fun LongArray.sort(noinline comparison: (a: Long, b: Long) -> Int)(source)
inline fun FloatArray.sort(noinline comparison: (a: Float, b: Float) -> Int)(source)
inline fun DoubleArray.sort(noinline comparison: (a: Double, b: Double) -> Int)(source)
inline fun CharArray.sort(noinline comparison: (a: Char, b: Char) -> Int)(source)

Deprecated

Warning since 1.6

Use other sorting functions from the Standard Library

Sorts the array in-place according to the order specified by the given comparison function.

Since Kotlin

1.1
fun <T> Array<out T>.sort()(source)

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

The sort is stable. It means that equal elements preserve their order relative to each other after sorting.

Since Kotlin

1.0

Throws

if any element of the array is not Comparable.


fun <T> Array<out T>.sort(fromIndex: Int = 0, toIndex: Int = size)(source)

Sorts a range in the array in-place.

The sort is stable. It means that equal elements preserve their order relative to each other after sorting.

Since Kotlin

1.0

Parameters

fromIndex

the start of the range (inclusive) to sort, 0 by default.

toIndex

the end of the range (exclusive) to sort, size of this array by default.

Throws

if fromIndex is less than zero or toIndex is greater than the size of this array.

Samples

import kotlin.test.*

fun main() { 
   //sampleStart 
   class Person(val firstName: String, val lastName: String) : Comparable<Person> {
    override fun compareTo(other: Person): Int = this.lastName.compareTo(other.lastName)
    override fun toString(): String = "$firstName $lastName"
}

val people = arrayOf(
    Person("Ragnar", "Lodbrok"),
    Person("Bjorn", "Ironside"),
    Person("Sweyn", "Forkbeard")
)

// before sorting
println(people.joinToString()) // Ragnar Lodbrok, Bjorn Ironside, Sweyn Forkbeard

people.sort(0, 2)

// after sorting
println(people.joinToString()) // Bjorn Ironside, Ragnar Lodbrok, Sweyn Forkbeard 
   //sampleEnd
}

actual fun IntArray.sort()(source)
actual fun LongArray.sort()(source)
actual fun ByteArray.sort()(source)
actual fun ShortArray.sort()(source)
actual fun DoubleArray.sort()(source)
actual fun FloatArray.sort()(source)
actual fun CharArray.sort()(source)

Sorts the array in-place.

Since Kotlin

1.0

Samples

import kotlin.test.*

fun main() { 
   //sampleStart 
   val intArray = intArrayOf(4, 3, 2, 1)

// before sorting
println(intArray.joinToString()) // 4, 3, 2, 1

intArray.sort()

// after sorting
println(intArray.joinToString()) // 1, 2, 3, 4 
   //sampleEnd
}

actual inline fun <T : Comparable<T>> Array<out T>.sort()(source)

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

The sort is stable. It means that equal elements preserve their order relative to each other after sorting.

Since Kotlin

1.0

Samples

import kotlin.test.*

fun main() { 
   //sampleStart 
   class Person(val firstName: String, val lastName: String) : Comparable<Person> {
    override fun compareTo(other: Person): Int = this.lastName.compareTo(other.lastName)
    override fun toString(): String = "$firstName $lastName"
}

val people = arrayOf(
    Person("Ragnar", "Lodbrok"),
    Person("Bjorn", "Ironside"),
    Person("Sweyn", "Forkbeard")
)

// before sorting
println(people.joinToString()) // Ragnar Lodbrok, Bjorn Ironside, Sweyn Forkbeard

people.sort()

// after sorting
println(people.joinToString()) // Sweyn Forkbeard, Bjorn Ironside, Ragnar Lodbrok 
   //sampleEnd
}

actual fun <T : Comparable<T>> Array<out T>.sort(fromIndex: Int = 0, toIndex: Int = size)(source)

Sorts a range in the array in-place.

The sort is stable. It means that equal elements preserve their order relative to each other after sorting.

Since Kotlin

1.4

Parameters

fromIndex

the start of the range (inclusive) to sort, 0 by default.

toIndex

the end of the range (exclusive) to sort, size of this array by default.

Throws

if fromIndex is less than zero or toIndex is greater than the size of this array.

Samples

import kotlin.test.*

fun main() { 
   //sampleStart 
   class Person(val firstName: String, val lastName: String) : Comparable<Person> {
    override fun compareTo(other: Person): Int = this.lastName.compareTo(other.lastName)
    override fun toString(): String = "$firstName $lastName"
}

val people = arrayOf(
    Person("Ragnar", "Lodbrok"),
    Person("Bjorn", "Ironside"),
    Person("Sweyn", "Forkbeard")
)

// before sorting
println(people.joinToString()) // Ragnar Lodbrok, Bjorn Ironside, Sweyn Forkbeard

people.sort(0, 2)

// after sorting
println(people.joinToString()) // Bjorn Ironside, Ragnar Lodbrok, Sweyn Forkbeard 
   //sampleEnd
}

actual fun ByteArray.sort(fromIndex: Int = 0, toIndex: Int = size)(source)
actual fun ShortArray.sort(fromIndex: Int = 0, toIndex: Int = size)(source)
actual fun IntArray.sort(fromIndex: Int = 0, toIndex: Int = size)(source)
actual fun LongArray.sort(fromIndex: Int = 0, toIndex: Int = size)(source)
actual fun FloatArray.sort(fromIndex: Int = 0, toIndex: Int = size)(source)
actual fun DoubleArray.sort(fromIndex: Int = 0, toIndex: Int = size)(source)
actual fun CharArray.sort(fromIndex: Int = 0, toIndex: Int = size)(source)

Sorts a range in the array in-place.

Since Kotlin

1.0

Parameters

fromIndex

the start of the range (inclusive) to sort, 0 by default.

toIndex

the end of the range (exclusive) to sort, size of this array by default.

Throws

if fromIndex is less than zero or toIndex is greater than the size of this array.

Samples

import kotlin.test.*

fun main() { 
   //sampleStart 
   val intArray = intArrayOf(4, 3, 2, 1)

// before sorting
println(intArray.joinToString()) // 4, 3, 2, 1

intArray.sort(0, 3)

// after sorting
println(intArray.joinToString()) // 2, 3, 4, 1 
   //sampleEnd
}

actual fun <T : Comparable<T>> MutableList<T>.sort()(source)

Sorts elements in the list in-place according to their natural sort order.

The sort is stable. It means that equal elements preserve their order relative to each other after sorting.

Since Kotlin

1.0

Samples

import kotlin.test.*

fun main() { 
   //sampleStart 
   val mutableList = mutableListOf(4, 3, 2, 1)

// before sorting
println(mutableList.joinToString()) // 4, 3, 2, 1

mutableList.sort()

// after sorting
println(mutableList.joinToString()) // 1, 2, 3, 4 
   //sampleEnd
}

inline fun <T> MutableList<T>.sort(comparator: Comparator<in T>)(source)

Deprecated (with error)

Use sortWith(comparator) instead.

Replace with

this.sortWith(comparator)

Since Kotlin

1.0

inline fun <T> MutableList<T>.sort(comparison: (T, T) -> Int)(source)

Deprecated (with error)

Use sortWith(Comparator(comparison)) instead.

Replace with

this.sortWith(Comparator(comparison))

Since Kotlin

1.0
actual fun IntArray.sort()(source)
actual fun LongArray.sort()(source)
actual fun ByteArray.sort()(source)
actual fun ShortArray.sort()(source)
actual fun DoubleArray.sort()(source)
actual fun FloatArray.sort()(source)
actual fun CharArray.sort()(source)

Sorts the array in-place.

Since Kotlin

1.3

Samples

import kotlin.test.*

fun main() { 
   //sampleStart 
   val intArray = intArrayOf(4, 3, 2, 1)

// before sorting
println(intArray.joinToString()) // 4, 3, 2, 1

intArray.sort()

// after sorting
println(intArray.joinToString()) // 1, 2, 3, 4 
   //sampleEnd
}

actual fun <T : Comparable<T>> Array<out T>.sort()(source)

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

The sort is stable. It means that equal elements preserve their order relative to each other after sorting.

Since Kotlin

1.3

Samples

import kotlin.test.*

fun main() { 
   //sampleStart 
   class Person(val firstName: String, val lastName: String) : Comparable<Person> {
    override fun compareTo(other: Person): Int = this.lastName.compareTo(other.lastName)
    override fun toString(): String = "$firstName $lastName"
}

val people = arrayOf(
    Person("Ragnar", "Lodbrok"),
    Person("Bjorn", "Ironside"),
    Person("Sweyn", "Forkbeard")
)

// before sorting
println(people.joinToString()) // Ragnar Lodbrok, Bjorn Ironside, Sweyn Forkbeard

people.sort()

// after sorting
println(people.joinToString()) // Sweyn Forkbeard, Bjorn Ironside, Ragnar Lodbrok 
   //sampleEnd
}

actual fun <T : Comparable<T>> Array<out T>.sort(fromIndex: Int = 0, toIndex: Int = size)(source)

Sorts a range in the array in-place.

The sort is stable. It means that equal elements preserve their order relative to each other after sorting.

Since Kotlin

1.4

Parameters

fromIndex

the start of the range (inclusive) to sort, 0 by default.

toIndex

the end of the range (exclusive) to sort, size of this array by default.

Throws

if fromIndex is less than zero or toIndex is greater than the size of this array.

Samples

import kotlin.test.*

fun main() { 
   //sampleStart 
   class Person(val firstName: String, val lastName: String) : Comparable<Person> {
    override fun compareTo(other: Person): Int = this.lastName.compareTo(other.lastName)
    override fun toString(): String = "$firstName $lastName"
}

val people = arrayOf(
    Person("Ragnar", "Lodbrok"),
    Person("Bjorn", "Ironside"),
    Person("Sweyn", "Forkbeard")
)

// before sorting
println(people.joinToString()) // Ragnar Lodbrok, Bjorn Ironside, Sweyn Forkbeard

people.sort(0, 2)

// after sorting
println(people.joinToString()) // Bjorn Ironside, Ragnar Lodbrok, Sweyn Forkbeard 
   //sampleEnd
}

actual fun ByteArray.sort(fromIndex: Int = 0, toIndex: Int = size)(source)
actual fun ShortArray.sort(fromIndex: Int = 0, toIndex: Int = size)(source)
actual fun IntArray.sort(fromIndex: Int = 0, toIndex: Int = size)(source)
actual fun LongArray.sort(fromIndex: Int = 0, toIndex: Int = size)(source)
actual fun FloatArray.sort(fromIndex: Int = 0, toIndex: Int = size)(source)
actual fun DoubleArray.sort(fromIndex: Int = 0, toIndex: Int = size)(source)
actual fun CharArray.sort(fromIndex: Int = 0, toIndex: Int = size)(source)

Sorts a range in the array in-place.

Since Kotlin

1.4

Parameters

fromIndex

the start of the range (inclusive) to sort, 0 by default.

toIndex

the end of the range (exclusive) to sort, size of this array by default.

Throws

if fromIndex is less than zero or toIndex is greater than the size of this array.

Samples

import kotlin.test.*

fun main() { 
   //sampleStart 
   val intArray = intArrayOf(4, 3, 2, 1)

// before sorting
println(intArray.joinToString()) // 4, 3, 2, 1

intArray.sort(0, 3)

// after sorting
println(intArray.joinToString()) // 2, 3, 4, 1 
   //sampleEnd
}

actual fun <T : Comparable<T>> MutableList<T>.sort()(source)

Sorts elements in the list in-place according to their natural sort order.

The sort is stable. It means that equal elements preserve their order relative to each other after sorting.

Since Kotlin

1.3

Samples

import kotlin.test.*

fun main() { 
   //sampleStart 
   val mutableList = mutableListOf(4, 3, 2, 1)

// before sorting
println(mutableList.joinToString()) // 4, 3, 2, 1

mutableList.sort()

// after sorting
println(mutableList.joinToString()) // 1, 2, 3, 4 
   //sampleEnd
}
actual fun IntArray.sort()(source)
actual fun LongArray.sort()(source)
actual fun ByteArray.sort()(source)
actual fun ShortArray.sort()(source)
actual fun DoubleArray.sort()(source)
actual fun FloatArray.sort()(source)
actual fun CharArray.sort()(source)

Sorts the array in-place.

Since Kotlin

1.8

Samples

import kotlin.test.*

fun main() { 
   //sampleStart 
   val intArray = intArrayOf(4, 3, 2, 1)

// before sorting
println(intArray.joinToString()) // 4, 3, 2, 1

intArray.sort()

// after sorting
println(intArray.joinToString()) // 1, 2, 3, 4 
   //sampleEnd
}

actual fun <T : Comparable<T>> Array<out T>.sort()(source)

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

The sort is stable. It means that equal elements preserve their order relative to each other after sorting.

Since Kotlin

1.8

Samples

import kotlin.test.*

fun main() { 
   //sampleStart 
   class Person(val firstName: String, val lastName: String) : Comparable<Person> {
    override fun compareTo(other: Person): Int = this.lastName.compareTo(other.lastName)
    override fun toString(): String = "$firstName $lastName"
}

val people = arrayOf(
    Person("Ragnar", "Lodbrok"),
    Person("Bjorn", "Ironside"),
    Person("Sweyn", "Forkbeard")
)

// before sorting
println(people.joinToString()) // Ragnar Lodbrok, Bjorn Ironside, Sweyn Forkbeard

people.sort()

// after sorting
println(people.joinToString()) // Sweyn Forkbeard, Bjorn Ironside, Ragnar Lodbrok 
   //sampleEnd
}

actual fun <T : Comparable<T>> Array<out T>.sort(fromIndex: Int = 0, toIndex: Int = size)(source)

Sorts a range in the array in-place.

The sort is stable. It means that equal elements preserve their order relative to each other after sorting.

Since Kotlin

1.8

Parameters

fromIndex

the start of the range (inclusive) to sort, 0 by default.

toIndex

the end of the range (exclusive) to sort, size of this array by default.

Throws

if fromIndex is less than zero or toIndex is greater than the size of this array.

Samples

import kotlin.test.*

fun main() { 
   //sampleStart 
   class Person(val firstName: String, val lastName: String) : Comparable<Person> {
    override fun compareTo(other: Person): Int = this.lastName.compareTo(other.lastName)
    override fun toString(): String = "$firstName $lastName"
}

val people = arrayOf(
    Person("Ragnar", "Lodbrok"),
    Person("Bjorn", "Ironside"),
    Person("Sweyn", "Forkbeard")
)

// before sorting
println(people.joinToString()) // Ragnar Lodbrok, Bjorn Ironside, Sweyn Forkbeard

people.sort(0, 2)

// after sorting
println(people.joinToString()) // Bjorn Ironside, Ragnar Lodbrok, Sweyn Forkbeard 
   //sampleEnd
}

actual fun ByteArray.sort(fromIndex: Int = 0, toIndex: Int = size)(source)
actual fun ShortArray.sort(fromIndex: Int = 0, toIndex: Int = size)(source)
actual fun IntArray.sort(fromIndex: Int = 0, toIndex: Int = size)(source)
actual fun LongArray.sort(fromIndex: Int = 0, toIndex: Int = size)(source)
actual fun FloatArray.sort(fromIndex: Int = 0, toIndex: Int = size)(source)
actual fun DoubleArray.sort(fromIndex: Int = 0, toIndex: Int = size)(source)
actual fun CharArray.sort(fromIndex: Int = 0, toIndex: Int = size)(source)

Sorts a range in the array in-place.

Since Kotlin

1.8

Parameters

fromIndex

the start of the range (inclusive) to sort, 0 by default.

toIndex

the end of the range (exclusive) to sort, size of this array by default.

Throws

if fromIndex is less than zero or toIndex is greater than the size of this array.

Samples

import kotlin.test.*

fun main() { 
   //sampleStart 
   val intArray = intArrayOf(4, 3, 2, 1)

// before sorting
println(intArray.joinToString()) // 4, 3, 2, 1

intArray.sort(0, 3)

// after sorting
println(intArray.joinToString()) // 2, 3, 4, 1 
   //sampleEnd
}

actual fun <T : Comparable<T>> MutableList<T>.sort()(source)

Sorts elements in the list in-place according to their natural sort order.

The sort is stable. It means that equal elements preserve their order relative to each other after sorting.

Since Kotlin

1.8

Samples

import kotlin.test.*

fun main() { 
   //sampleStart 
   val mutableList = mutableListOf(4, 3, 2, 1)

// before sorting
println(mutableList.joinToString()) // 4, 3, 2, 1

mutableList.sort()

// after sorting
println(mutableList.joinToString()) // 1, 2, 3, 4 
   //sampleEnd
}
actual fun IntArray.sort()(source)
actual fun LongArray.sort()(source)
actual fun ByteArray.sort()(source)
actual fun ShortArray.sort()(source)
actual fun DoubleArray.sort()(source)
actual fun FloatArray.sort()(source)
actual fun CharArray.sort()(source)

Sorts the array in-place.

Since Kotlin

1.8

Samples

import kotlin.test.*

fun main() { 
   //sampleStart 
   val intArray = intArrayOf(4, 3, 2, 1)

// before sorting
println(intArray.joinToString()) // 4, 3, 2, 1

intArray.sort()

// after sorting
println(intArray.joinToString()) // 1, 2, 3, 4 
   //sampleEnd
}

actual fun <T : Comparable<T>> Array<out T>.sort()(source)

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

The sort is stable. It means that equal elements preserve their order relative to each other after sorting.

Since Kotlin

1.8

Samples

import kotlin.test.*

fun main() { 
   //sampleStart 
   class Person(val firstName: String, val lastName: String) : Comparable<Person> {
    override fun compareTo(other: Person): Int = this.lastName.compareTo(other.lastName)
    override fun toString(): String = "$firstName $lastName"
}

val people = arrayOf(
    Person("Ragnar", "Lodbrok"),
    Person("Bjorn", "Ironside"),
    Person("Sweyn", "Forkbeard")
)

// before sorting
println(people.joinToString()) // Ragnar Lodbrok, Bjorn Ironside, Sweyn Forkbeard

people.sort()

// after sorting
println(people.joinToString()) // Sweyn Forkbeard, Bjorn Ironside, Ragnar Lodbrok 
   //sampleEnd
}

actual fun <T : Comparable<T>> Array<out T>.sort(fromIndex: Int = 0, toIndex: Int = size)(source)

Sorts a range in the array in-place.

The sort is stable. It means that equal elements preserve their order relative to each other after sorting.

Since Kotlin

1.8

Parameters

fromIndex

the start of the range (inclusive) to sort, 0 by default.

toIndex

the end of the range (exclusive) to sort, size of this array by default.

Throws

if fromIndex is less than zero or toIndex is greater than the size of this array.

Samples

import kotlin.test.*

fun main() { 
   //sampleStart 
   class Person(val firstName: String, val lastName: String) : Comparable<Person> {
    override fun compareTo(other: Person): Int = this.lastName.compareTo(other.lastName)
    override fun toString(): String = "$firstName $lastName"
}

val people = arrayOf(
    Person("Ragnar", "Lodbrok"),
    Person("Bjorn", "Ironside"),
    Person("Sweyn", "Forkbeard")
)

// before sorting
println(people.joinToString()) // Ragnar Lodbrok, Bjorn Ironside, Sweyn Forkbeard

people.sort(0, 2)

// after sorting
println(people.joinToString()) // Bjorn Ironside, Ragnar Lodbrok, Sweyn Forkbeard 
   //sampleEnd
}

actual fun ByteArray.sort(fromIndex: Int = 0, toIndex: Int = size)(source)
actual fun ShortArray.sort(fromIndex: Int = 0, toIndex: Int = size)(source)
actual fun IntArray.sort(fromIndex: Int = 0, toIndex: Int = size)(source)
actual fun LongArray.sort(fromIndex: Int = 0, toIndex: Int = size)(source)
actual fun FloatArray.sort(fromIndex: Int = 0, toIndex: Int = size)(source)
actual fun DoubleArray.sort(fromIndex: Int = 0, toIndex: Int = size)(source)
actual fun CharArray.sort(fromIndex: Int = 0, toIndex: Int = size)(source)

Sorts a range in the array in-place.

Since Kotlin

1.8

Parameters

fromIndex

the start of the range (inclusive) to sort, 0 by default.

toIndex

the end of the range (exclusive) to sort, size of this array by default.

Throws

if fromIndex is less than zero or toIndex is greater than the size of this array.

Samples

import kotlin.test.*

fun main() { 
   //sampleStart 
   val intArray = intArrayOf(4, 3, 2, 1)

// before sorting
println(intArray.joinToString()) // 4, 3, 2, 1

intArray.sort(0, 3)

// after sorting
println(intArray.joinToString()) // 2, 3, 4, 1 
   //sampleEnd
}

actual fun <T : Comparable<T>> MutableList<T>.sort()(source)

Sorts elements in the list in-place according to their natural sort order.

The sort is stable. It means that equal elements preserve their order relative to each other after sorting.

Since Kotlin

1.8

Samples

import kotlin.test.*

fun main() { 
   //sampleStart 
   val mutableList = mutableListOf(4, 3, 2, 1)

// before sorting
println(mutableList.joinToString()) // 4, 3, 2, 1

mutableList.sort()

// after sorting
println(mutableList.joinToString()) // 1, 2, 3, 4 
   //sampleEnd
}