sort

Common
fun UIntArray.sort()
fun ULongArray.sort()
fun UByteArray.sort()
fun UShortArray.sort()

Sorts the array in-place.

Since Kotlin

1.3

Samples

import samples.*
import kotlin.test.*
fun main() { 
   //sampleStart 
   val intArray = intArrayOf(4, 3, 2, 1)

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

intArray.sort()

// after sorting
assertPrints(intArray.joinToString(), "1, 2, 3, 4") 
   //sampleEnd
}
Common
fun UIntArray.sort(fromIndex: Int = 0, toIndex: Int = size)
fun ULongArray.sort(fromIndex: Int = 0, toIndex: Int = size)
fun UByteArray.sort(fromIndex: Int = 0, toIndex: Int = size)
fun UShortArray.sort(fromIndex: Int = 0, toIndex: Int = size)
fun ByteArray.sort(fromIndex: Int, toIndex: Int)
fun ShortArray.sort(fromIndex: Int, toIndex: Int)
fun IntArray.sort(fromIndex: Int, toIndex: Int)
fun LongArray.sort(fromIndex: Int, toIndex: Int)
fun FloatArray.sort(fromIndex: Int, toIndex: Int)
fun DoubleArray.sort(fromIndex: Int, toIndex: Int)
fun CharArray.sort(fromIndex: Int, toIndex: Int)

Sorts a range in the array in-place.

Since Kotlin

1.4

Samples

import samples.*
import kotlin.test.*
fun main() { 
   //sampleStart 
   val intArray = intArrayOf(4, 3, 2, 1)

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

intArray.sort(0, 3)

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

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.

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

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.

Throws

if any element of the array is not Comparable.

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

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.

Samples

import 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
assertPrints(people.joinToString(), "Ragnar Lodbrok, Bjorn Ironside, Sweyn Forkbeard")

people.sort(0, 2)

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

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.

inline fun <T> MutableList<T>.sort(comparator: Comparator<in T>)
inline fun <T> MutableList<T>.sort(comparison: (T, T) -> Int)
JS
fun <T> Array<out T>.sort(comparison: (T, T) -> Int)

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.

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

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

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

Sorts the array in-place.

Samples

import samples.*
import kotlin.test.*
fun main() { 
   //sampleStart 
   val intArray = intArrayOf(4, 3, 2, 1)

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

intArray.sort()

// after sorting
assertPrints(intArray.joinToString(), "1, 2, 3, 4") 
   //sampleEnd
}
fun IntArray.sort()
fun LongArray.sort()
fun ByteArray.sort()
fun ShortArray.sort()
fun DoubleArray.sort()
fun FloatArray.sort()
fun CharArray.sort()

Sorts the array in-place.

Samples

import samples.*
import kotlin.test.*
fun main() { 
   //sampleStart 
   val intArray = intArrayOf(4, 3, 2, 1)

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

intArray.sort()

// after sorting
assertPrints(intArray.joinToString(), "1, 2, 3, 4") 
   //sampleEnd
}
JS
fun IntArray.sort()
fun LongArray.sort()
fun ByteArray.sort()
fun ShortArray.sort()
fun DoubleArray.sort()
fun FloatArray.sort()
fun CharArray.sort()

Sorts the array in-place.

Samples

import samples.*
import kotlin.test.*
fun main() { 
   //sampleStart 
   val intArray = intArrayOf(4, 3, 2, 1)

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

intArray.sort()

// after sorting
assertPrints(intArray.joinToString(), "1, 2, 3, 4") 
   //sampleEnd
}
Native
fun IntArray.sort()
fun LongArray.sort()
fun ByteArray.sort()
fun ShortArray.sort()
fun DoubleArray.sort()
fun FloatArray.sort()
fun CharArray.sort()

Sorts the array in-place.

Samples

import samples.*
import kotlin.test.*
fun main() { 
   //sampleStart 
   val intArray = intArrayOf(4, 3, 2, 1)

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

intArray.sort()

// after sorting
assertPrints(intArray.joinToString(), "1, 2, 3, 4") 
   //sampleEnd
}
Common
fun <T : Comparable<T>> Array<out T>.sort()

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.

Samples

import 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
assertPrints(people.joinToString(), "Ragnar Lodbrok, Bjorn Ironside, Sweyn Forkbeard")

people.sort()

// after sorting
assertPrints(people.joinToString(), "Sweyn Forkbeard, Bjorn Ironside, Ragnar Lodbrok") 
   //sampleEnd
}
inline fun <T : Comparable<T>> Array<out T>.sort()

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.

Samples

import 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
assertPrints(people.joinToString(), "Ragnar Lodbrok, Bjorn Ironside, Sweyn Forkbeard")

people.sort()

// after sorting
assertPrints(people.joinToString(), "Sweyn Forkbeard, Bjorn Ironside, Ragnar Lodbrok") 
   //sampleEnd
}
JS
fun <T : Comparable<T>> Array<out T>.sort()

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.

Samples

import 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
assertPrints(people.joinToString(), "Ragnar Lodbrok, Bjorn Ironside, Sweyn Forkbeard")

people.sort()

// after sorting
assertPrints(people.joinToString(), "Sweyn Forkbeard, Bjorn Ironside, Ragnar Lodbrok") 
   //sampleEnd
}
Native
fun <T : Comparable<T>> Array<out T>.sort()

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.

Samples

import 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
assertPrints(people.joinToString(), "Ragnar Lodbrok, Bjorn Ironside, Sweyn Forkbeard")

people.sort()

// after sorting
assertPrints(people.joinToString(), "Sweyn Forkbeard, Bjorn Ironside, Ragnar Lodbrok") 
   //sampleEnd
}
Common
fun <T : Comparable<T>> Array<out T>.sort(fromIndex: Int, toIndex: Int)

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

Samples

import 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
assertPrints(people.joinToString(), "Ragnar Lodbrok, Bjorn Ironside, Sweyn Forkbeard")

people.sort(0, 2)

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

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.

fun <T : Comparable<T>> Array<out T>.sort(fromIndex: Int, toIndex: Int)

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

Samples

import 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
assertPrints(people.joinToString(), "Ragnar Lodbrok, Bjorn Ironside, Sweyn Forkbeard")

people.sort(0, 2)

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

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.

JS
fun <T : Comparable<T>> Array<out T>.sort(fromIndex: Int, toIndex: Int)

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

Samples

import 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
assertPrints(people.joinToString(), "Ragnar Lodbrok, Bjorn Ironside, Sweyn Forkbeard")

people.sort(0, 2)

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

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.

Native
fun <T : Comparable<T>> Array<out T>.sort(fromIndex: Int, toIndex: Int)

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

Samples

import 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
assertPrints(people.joinToString(), "Ragnar Lodbrok, Bjorn Ironside, Sweyn Forkbeard")

people.sort(0, 2)

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

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.

fun ByteArray.sort(fromIndex: Int, toIndex: Int)
fun ShortArray.sort(fromIndex: Int, toIndex: Int)
fun IntArray.sort(fromIndex: Int, toIndex: Int)
fun LongArray.sort(fromIndex: Int, toIndex: Int)
fun FloatArray.sort(fromIndex: Int, toIndex: Int)
fun DoubleArray.sort(fromIndex: Int, toIndex: Int)
fun CharArray.sort(fromIndex: Int, toIndex: Int)

Sorts a range in the array in-place.

Samples

import samples.*
import kotlin.test.*
fun main() { 
   //sampleStart 
   val intArray = intArrayOf(4, 3, 2, 1)

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

intArray.sort(0, 3)

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

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.

JS
fun ByteArray.sort(fromIndex: Int, toIndex: Int)
fun ShortArray.sort(fromIndex: Int, toIndex: Int)
fun IntArray.sort(fromIndex: Int, toIndex: Int)
fun LongArray.sort(fromIndex: Int, toIndex: Int)
fun FloatArray.sort(fromIndex: Int, toIndex: Int)
fun DoubleArray.sort(fromIndex: Int, toIndex: Int)
fun CharArray.sort(fromIndex: Int, toIndex: Int)

Sorts a range in the array in-place.

Since Kotlin

1.4

Samples

import samples.*
import kotlin.test.*
fun main() { 
   //sampleStart 
   val intArray = intArrayOf(4, 3, 2, 1)

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

intArray.sort(0, 3)

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

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.

Native
fun ByteArray.sort(fromIndex: Int, toIndex: Int)
fun ShortArray.sort(fromIndex: Int, toIndex: Int)
fun IntArray.sort(fromIndex: Int, toIndex: Int)
fun LongArray.sort(fromIndex: Int, toIndex: Int)
fun FloatArray.sort(fromIndex: Int, toIndex: Int)
fun DoubleArray.sort(fromIndex: Int, toIndex: Int)
fun CharArray.sort(fromIndex: Int, toIndex: Int)

Sorts a range in the array in-place.

Since Kotlin

1.4

Samples

import samples.*
import kotlin.test.*
fun main() { 
   //sampleStart 
   val intArray = intArrayOf(4, 3, 2, 1)

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

intArray.sort(0, 3)

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

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.

Common
fun <T : Comparable<T>> MutableList<T>.sort()

Samples

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

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.

Samples

import samples.*
import kotlin.test.*
fun main() { 
   //sampleStart 
   val mutableList = mutableListOf(4, 3, 2, 1)

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

mutableList.sort()

// after sorting
assertPrints(mutableList.joinToString(), "1, 2, 3, 4") 
   //sampleEnd
}
JS
fun <T : Comparable<T>> MutableList<T>.sort()

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.

Samples

Native
fun <T : Comparable<T>> MutableList<T>.sort()

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.

Samples