isSortedWith

fun <T> Array<out T>.isSortedWith(comparator: Comparator<in T>): Boolean(source)

Returns true if each element in the array is less than or equal to the following element according to the specified comparator.

Returns true if the array has fewer than two elements.

The elements are compared sequentially using Comparator.compare, and the array is considered sorted if for each pair of adjacent elements the preceding element is not greater than the following one.

Since Kotlin

2.4

Samples


fun main() { 
   //sampleStart 
   println(arrayOf<String>().isSortedWith(naturalOrder())) // true
println(arrayOf("apple").isSortedWith(naturalOrder())) // true

val sorted = arrayOf("apple", "banana", "cherry")
println(sorted.isSortedWith(naturalOrder())) // true
println(sorted.isSortedWith(reverseOrder())) // false

val reversed = arrayOf("cherry", "banana", "apple")
println(reversed.isSortedWith(reverseOrder())) // true

val caseInsensitive = arrayOf("Apple", "banana", "Cherry")
println(caseInsensitive.isSortedWith(String.CASE_INSENSITIVE_ORDER)) // true

val withNulls = arrayOf(null, "apple", "banana")
println(withNulls.isSortedWith(nullsFirst(naturalOrder()))) // true
println(withNulls.isSortedWith(nullsLast(naturalOrder()))) // false 
   //sampleEnd
}

Returns true if each element in the array is less than or equal to the following element according to the specified comparator.

Returns true if the array has fewer than two elements.

The elements are compared sequentially using Comparator.compare, and the array is considered sorted if for each pair of adjacent elements the preceding element is not greater than the following one.

Since Kotlin

2.4

Samples

import kotlin.math.abs

fun main() { 
   //sampleStart 
   println(byteArrayOf().isSortedWith(naturalOrder())) // true
println(byteArrayOf(1).isSortedWith(naturalOrder())) // true

val sorted = byteArrayOf(1, 2, 3, 4, 5)
println(sorted.isSortedWith(naturalOrder())) // true
println(sorted.isSortedWith(reverseOrder())) // false

val reversed = byteArrayOf(5, 4, 3, 2, 1)
println(reversed.isSortedWith(reverseOrder())) // true 
   //sampleEnd
}

Returns true if each element in the array is less than or equal to the following element according to the specified comparator.

Returns true if the array has fewer than two elements.

The elements are compared sequentially using Comparator.compare, and the array is considered sorted if for each pair of adjacent elements the preceding element is not greater than the following one.

Since Kotlin

2.4

Samples

import kotlin.math.abs

fun main() { 
   //sampleStart 
   println(shortArrayOf().isSortedWith(naturalOrder())) // true
println(shortArrayOf(1).isSortedWith(naturalOrder())) // true

val sorted = shortArrayOf(1, 2, 3, 4, 5)
println(sorted.isSortedWith(naturalOrder())) // true
println(sorted.isSortedWith(reverseOrder())) // false

val reversed = shortArrayOf(5, 4, 3, 2, 1)
println(reversed.isSortedWith(reverseOrder())) // true 
   //sampleEnd
}

Returns true if each element in the array is less than or equal to the following element according to the specified comparator.

Returns true if the array has fewer than two elements.

The elements are compared sequentially using Comparator.compare, and the array is considered sorted if for each pair of adjacent elements the preceding element is not greater than the following one.

Since Kotlin

2.4

Samples

import kotlin.math.abs

fun main() { 
   //sampleStart 
   println(intArrayOf().isSortedWith(naturalOrder())) // true
println(intArrayOf(1).isSortedWith(naturalOrder())) // true

val sorted = intArrayOf(1, 2, 3, 4, 5)
println(sorted.isSortedWith(naturalOrder())) // true
println(sorted.isSortedWith(reverseOrder())) // false

val reversed = intArrayOf(5, 4, 3, 2, 1)
println(reversed.isSortedWith(reverseOrder())) // true 
   //sampleEnd
}

Returns true if each element in the array is less than or equal to the following element according to the specified comparator.

Returns true if the array has fewer than two elements.

The elements are compared sequentially using Comparator.compare, and the array is considered sorted if for each pair of adjacent elements the preceding element is not greater than the following one.

Since Kotlin

2.4

Samples

import kotlin.math.abs

fun main() { 
   //sampleStart 
   println(longArrayOf().isSortedWith(naturalOrder())) // true
println(longArrayOf(1L).isSortedWith(naturalOrder())) // true

val sorted = longArrayOf(1L, 2L, 3L, 4L, 5L)
println(sorted.isSortedWith(naturalOrder())) // true
println(sorted.isSortedWith(reverseOrder())) // false

val reversed = longArrayOf(5L, 4L, 3L, 2L, 1L)
println(reversed.isSortedWith(reverseOrder())) // true 
   //sampleEnd
}

Returns true if each element in the array is less than or equal to the following element according to the specified comparator.

Returns true if the array has fewer than two elements.

The elements are compared sequentially using Comparator.compare, and the array is considered sorted if for each pair of adjacent elements the preceding element is not greater than the following one.

Since Kotlin

2.4

Samples

import kotlin.math.abs

fun main() { 
   //sampleStart 
   println(floatArrayOf().isSortedWith(naturalOrder())) // true
println(floatArrayOf(1.0f).isSortedWith(naturalOrder())) // true

val sorted = floatArrayOf(1.0f, 2.5f, 3.14f)
println(sorted.isSortedWith(naturalOrder())) // true
println(sorted.isSortedWith(reverseOrder())) // false

val reversed = floatArrayOf(3.14f, 2.5f, 1.0f)
println(reversed.isSortedWith(reverseOrder())) // true 
   //sampleEnd
}

Returns true if each element in the array is less than or equal to the following element according to the specified comparator.

Returns true if the array has fewer than two elements.

The elements are compared sequentially using Comparator.compare, and the array is considered sorted if for each pair of adjacent elements the preceding element is not greater than the following one.

Since Kotlin

2.4

Samples

import kotlin.math.abs

fun main() { 
   //sampleStart 
   println(doubleArrayOf().isSortedWith(naturalOrder())) // true
println(doubleArrayOf(1.0).isSortedWith(naturalOrder())) // true

val sorted = doubleArrayOf(1.0, 2.5, 3.14)
println(sorted.isSortedWith(naturalOrder())) // true
println(sorted.isSortedWith(reverseOrder())) // false

val reversed = doubleArrayOf(3.14, 2.5, 1.0)
println(reversed.isSortedWith(reverseOrder())) // true 
   //sampleEnd
}

Returns true if each element in the array is less than or equal to the following element according to the specified comparator.

Returns true if the array has fewer than two elements.

The elements are compared sequentially using Comparator.compare, and the array is considered sorted if for each pair of adjacent elements the preceding element is not greater than the following one.

Since Kotlin

2.4

Samples


fun main() { 
   //sampleStart 
   println(booleanArrayOf().isSortedWith(naturalOrder())) // true
println(booleanArrayOf(false).isSortedWith(naturalOrder())) // true

val sorted = booleanArrayOf(false, false, true)
println(sorted.isSortedWith(naturalOrder())) // true
println(sorted.isSortedWith(reverseOrder())) // false

val reversed = booleanArrayOf(true, false, false)
println(reversed.isSortedWith(reverseOrder())) // true 
   //sampleEnd
}

Returns true if each element in the array is less than or equal to the following element according to the specified comparator.

Returns true if the array has fewer than two elements.

The elements are compared sequentially using Comparator.compare, and the array is considered sorted if for each pair of adjacent elements the preceding element is not greater than the following one.

Since Kotlin

2.4

Samples


fun main() { 
   //sampleStart 
   println(charArrayOf().isSortedWith(naturalOrder())) // true
println(charArrayOf('a').isSortedWith(naturalOrder())) // true

val sorted = charArrayOf('a', 'b', 'c')
println(sorted.isSortedWith(naturalOrder())) // true
println(sorted.isSortedWith(reverseOrder())) // false

val reversed = charArrayOf('c', 'b', 'a')
println(reversed.isSortedWith(reverseOrder())) // true 
   //sampleEnd
}

fun <T> Iterable<T>.isSortedWith(comparator: Comparator<in T>): Boolean(source)

Returns true if each element in the collection is less than or equal to the following element according to the specified comparator.

Returns true if the collection has fewer than two elements.

The elements are compared sequentially using Comparator.compare, and the collection is considered sorted if for each pair of adjacent elements the preceding element is not greater than the following one.

Note that the result depends on the iteration order of the collection. The iteration order of some Iterable implementations may be unstable (change from one invocation to the next), in which case this function may return inconsistent results.

Since Kotlin

2.4

Samples


fun main() { 
   //sampleStart 
   println(listOf<String>().isSortedWith(naturalOrder())) // true
println(listOf("apple").isSortedWith(naturalOrder())) // true

val sorted = listOf("apple", "banana", "cherry")
println(sorted.isSortedWith(naturalOrder())) // true
println(sorted.isSortedWith(reverseOrder())) // false

val reversed = listOf("cherry", "banana", "apple")
println(reversed.isSortedWith(reverseOrder())) // true

val caseInsensitive = listOf("Apple", "banana", "Cherry")
println(caseInsensitive.isSortedWith(String.CASE_INSENSITIVE_ORDER)) // true

val withNulls = listOf(null, "apple", "banana")
println(withNulls.isSortedWith(nullsFirst(naturalOrder()))) // true
println(withNulls.isSortedWith(nullsLast(naturalOrder()))) // false 
   //sampleEnd
}

Returns true if each element in the array is less than or equal to the following element according to the specified comparator.

Returns true if the array has fewer than two elements.

The elements are compared sequentially using Comparator.compare, and the array is considered sorted if for each pair of adjacent elements the preceding element is not greater than the following one.

Since Kotlin

2.4

Samples


fun main() { 
   //sampleStart 
   println(uintArrayOf().isSortedWith(naturalOrder())) // true
println(uintArrayOf(1u).isSortedWith(naturalOrder())) // true

val sorted = uintArrayOf(1u, 2u, 3u, 4u, 5u)
println(sorted.isSortedWith(naturalOrder())) // true
println(sorted.isSortedWith(reverseOrder())) // false

val reversed = uintArrayOf(5u, 4u, 3u, 2u, 1u)
println(reversed.isSortedWith(reverseOrder())) // true 
   //sampleEnd
}

Returns true if each element in the array is less than or equal to the following element according to the specified comparator.

Returns true if the array has fewer than two elements.

The elements are compared sequentially using Comparator.compare, and the array is considered sorted if for each pair of adjacent elements the preceding element is not greater than the following one.

Since Kotlin

2.4

Samples


fun main() { 
   //sampleStart 
   println(ulongArrayOf().isSortedWith(naturalOrder())) // true
println(ulongArrayOf(1uL).isSortedWith(naturalOrder())) // true

val sorted = ulongArrayOf(1uL, 2uL, 3uL, 4uL, 5uL)
println(sorted.isSortedWith(naturalOrder())) // true
println(sorted.isSortedWith(reverseOrder())) // false

val reversed = ulongArrayOf(5uL, 4uL, 3uL, 2uL, 1uL)
println(reversed.isSortedWith(reverseOrder())) // true 
   //sampleEnd
}

Returns true if each element in the array is less than or equal to the following element according to the specified comparator.

Returns true if the array has fewer than two elements.

The elements are compared sequentially using Comparator.compare, and the array is considered sorted if for each pair of adjacent elements the preceding element is not greater than the following one.

Since Kotlin

2.4

Samples


fun main() { 
   //sampleStart 
   println(ubyteArrayOf().isSortedWith(naturalOrder())) // true
println(ubyteArrayOf(1u).isSortedWith(naturalOrder())) // true

val sorted = ubyteArrayOf(1u, 2u, 3u, 4u, 5u)
println(sorted.isSortedWith(naturalOrder())) // true
println(sorted.isSortedWith(reverseOrder())) // false

val reversed = ubyteArrayOf(5u, 4u, 3u, 2u, 1u)
println(reversed.isSortedWith(reverseOrder())) // true 
   //sampleEnd
}

Returns true if each element in the array is less than or equal to the following element according to the specified comparator.

Returns true if the array has fewer than two elements.

The elements are compared sequentially using Comparator.compare, and the array is considered sorted if for each pair of adjacent elements the preceding element is not greater than the following one.

Since Kotlin

2.4

Samples


fun main() { 
   //sampleStart 
   println(ushortArrayOf().isSortedWith(naturalOrder())) // true
println(ushortArrayOf(1u).isSortedWith(naturalOrder())) // true

val sorted = ushortArrayOf(1u, 2u, 3u, 4u, 5u)
println(sorted.isSortedWith(naturalOrder())) // true
println(sorted.isSortedWith(reverseOrder())) // false

val reversed = ushortArrayOf(5u, 4u, 3u, 2u, 1u)
println(reversed.isSortedWith(reverseOrder())) // true 
   //sampleEnd
}