sortWith
fun
<
T
>
Array
<
out
T
>
.
sortWith
(
comparator
:
Comparator
<
in
T
>
)
(Common source) (JS source) (Native source)
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.
fun
<
T
>
Array
<
out
T
>
.
sortWith
(
comparator
:
Comparator
<
in
T
>
,
fromIndex
:
Int
=
0
,
toIndex
:
Int
=
size
)
(Common source) (JS source) (Native source)
Sorts 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.
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.
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
.
fun
<
T
>
MutableList
<
T
>
.
sortWith
(
comparator
:
Comparator
<
in
T
>
)
(Common source) (JS source) (Native source)
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.
import kotlin.test.*
fun main(args: Array<String>) {
//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.
fun
<
T
>
Array
<
out
T
>
.
sortWith
(
comparator
:
Comparator
<
in
T
>
,
fromIndex
:
Int
=
0
,
toIndex
:
Int
=
size
)
(source)
Sorts 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.
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.
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
.
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.
import kotlin.test.*
fun main(args: Array<String>) {
//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
}