sortWith
Sorts the array in-place according to the order specified by the given comparator.
The sort is stable. It means that equal elements preserve their order relative to each other after sorting.
Since Kotlin
1.0Sorts a range in the array in-place with the given comparator.
The sort is stable. It means that equal elements preserve their order relative to each other after sorting.
Since Kotlin
1.0Parameters
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 the order specified with comparator.
The sort is stable. It means that equal elements preserve their order relative to each other after sorting.
Since Kotlin
1.0Samples
import kotlin.test.*
fun main() {
//sampleStart
// non-comparable class
class Person(val firstName: String, val lastName: String) {
override fun toString(): String = "$firstName $lastName"
}
val people = mutableListOf(
Person("Ragnar", "Lodbrok"),
Person("Bjorn", "Ironside"),
Person("Sweyn", "Forkbeard")
)
people.sortWith(compareByDescending { it.firstName })
// after sorting
println(people.joinToString()) // Sweyn Forkbeard, Ragnar Lodbrok, Bjorn Ironside
//sampleEnd
}
Sorts the array in-place according to the order specified by the given comparator.
The sort is stable. It means that equal elements preserve their order relative to each other after sorting.
Since Kotlin
1.1Sorts a range in the array in-place with the given comparator.
The sort is stable. It means that equal elements preserve their order relative to each other after sorting.
Since Kotlin
1.4Parameters
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 the order specified with comparator.
The sort is stable. It means that equal elements preserve their order relative to each other after sorting.
Since Kotlin
1.1Samples
import kotlin.test.*
fun main() {
//sampleStart
// non-comparable class
class Person(val firstName: String, val lastName: String) {
override fun toString(): String = "$firstName $lastName"
}
val people = mutableListOf(
Person("Ragnar", "Lodbrok"),
Person("Bjorn", "Ironside"),
Person("Sweyn", "Forkbeard")
)
people.sortWith(compareByDescending { it.firstName })
// after sorting
println(people.joinToString()) // Sweyn Forkbeard, Ragnar Lodbrok, Bjorn Ironside
//sampleEnd
}
Sorts the array in-place according to the order specified by the given comparator.
The sort is stable. It means that equal elements preserve their order relative to each other after sorting.
Since Kotlin
1.0Sorts a range in the array in-place with the given comparator.
The sort is stable. It means that equal elements preserve their order relative to each other after sorting.
Since Kotlin
1.0Parameters
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 the order specified with comparator.
The sort is stable. It means that equal elements preserve their order relative to each other after sorting.
Since Kotlin
1.0Samples
import kotlin.test.*
fun main() {
//sampleStart
// non-comparable class
class Person(val firstName: String, val lastName: String) {
override fun toString(): String = "$firstName $lastName"
}
val people = mutableListOf(
Person("Ragnar", "Lodbrok"),
Person("Bjorn", "Ironside"),
Person("Sweyn", "Forkbeard")
)
people.sortWith(compareByDescending { it.firstName })
// after sorting
println(people.joinToString()) // Sweyn Forkbeard, Ragnar Lodbrok, Bjorn Ironside
//sampleEnd
}
Sorts the array in-place according to the order specified by the given comparator.
The sort is stable. It means that equal elements preserve their order relative to each other after sorting.
Since Kotlin
1.3Sorts a range in the array in-place with the given comparator.
The sort is stable. It means that equal elements preserve their order relative to each other after sorting.
Since Kotlin
1.3Parameters
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 the order specified with comparator.
The sort is stable. It means that equal elements preserve their order relative to each other after sorting.
Since Kotlin
1.3Samples
import kotlin.test.*
fun main() {
//sampleStart
// non-comparable class
class Person(val firstName: String, val lastName: String) {
override fun toString(): String = "$firstName $lastName"
}
val people = mutableListOf(
Person("Ragnar", "Lodbrok"),
Person("Bjorn", "Ironside"),
Person("Sweyn", "Forkbeard")
)
people.sortWith(compareByDescending { it.firstName })
// after sorting
println(people.joinToString()) // Sweyn Forkbeard, Ragnar Lodbrok, Bjorn Ironside
//sampleEnd
}
Sorts the array in-place according to the order specified by the given comparator.
The sort is stable. It means that equal elements preserve their order relative to each other after sorting.
Since Kotlin
1.8Sorts a range in the array in-place with the given comparator.
The sort is stable. It means that equal elements preserve their order relative to each other after sorting.
Since Kotlin
1.8Parameters
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 the order specified with comparator.
The sort is stable. It means that equal elements preserve their order relative to each other after sorting.
Since Kotlin
1.8Samples
import kotlin.test.*
fun main() {
//sampleStart
// non-comparable class
class Person(val firstName: String, val lastName: String) {
override fun toString(): String = "$firstName $lastName"
}
val people = mutableListOf(
Person("Ragnar", "Lodbrok"),
Person("Bjorn", "Ironside"),
Person("Sweyn", "Forkbeard")
)
people.sortWith(compareByDescending { it.firstName })
// after sorting
println(people.joinToString()) // Sweyn Forkbeard, Ragnar Lodbrok, Bjorn Ironside
//sampleEnd
}
Sorts the array in-place according to the order specified by the given comparator.
The sort is stable. It means that equal elements preserve their order relative to each other after sorting.
Since Kotlin
1.8Sorts a range in the array in-place with the given comparator.
The sort is stable. It means that equal elements preserve their order relative to each other after sorting.
Since Kotlin
1.8Parameters
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 the order specified with comparator.
The sort is stable. It means that equal elements preserve their order relative to each other after sorting.
Since Kotlin
1.8Samples
import kotlin.test.*
fun main() {
//sampleStart
// non-comparable class
class Person(val firstName: String, val lastName: String) {
override fun toString(): String = "$firstName $lastName"
}
val people = mutableListOf(
Person("Ragnar", "Lodbrok"),
Person("Bjorn", "Ironside"),
Person("Sweyn", "Forkbeard")
)
people.sortWith(compareByDescending { it.firstName })
// after sorting
println(people.joinToString()) // Sweyn Forkbeard, Ragnar Lodbrok, Bjorn Ironside
//sampleEnd
}