sort
Sorts the array in-place.
Since Kotlin
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
}
Sorts a range in the array in-place.
Since Kotlin
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
the start of the range (inclusive) to sort, 0 by default.
the end of the range (exclusive) to sort, size of this array by default.
Throws
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
}
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
}
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
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
the start of the range (inclusive) to sort, 0 by default.
the end of the range (exclusive) to sort, size of this array by default.
Throws
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.
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
the start of the range (inclusive) to sort, 0 by default.
the end of the range (exclusive) to sort, size of this array by default.
Throws
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
}
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
}
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
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
the start of the range (inclusive) to sort, 0 by default.
the end of the range (exclusive) to sort, size of this array by default.
Throws
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
the start of the range (inclusive) to sort, 0 by default.
the end of the range (exclusive) to sort, size of this array by default.
Throws
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
}
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.
Sorts the array in-place according to the order specified by the given comparison function.
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
}
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
}
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
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
the start of the range (inclusive) to sort, 0 by default.
the end of the range (exclusive) to sort, size of this array by default.
Throws
Sorts a range in the array in-place.
Since Kotlin
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
the start of the range (inclusive) to sort, 0 by default.
the end of the range (exclusive) to sort, size of this array by default.
Throws
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.
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
}
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
}
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
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
the start of the range (inclusive) to sort, 0 by default.
the end of the range (exclusive) to sort, size of this array by default.
Throws
Sorts a range in the array in-place.
Since Kotlin
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
the start of the range (inclusive) to sort, 0 by default.
the end of the range (exclusive) to sort, size of this array by default.
Throws
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.