Kotlin List binarySearchBy()
Syntax & Examples


Syntax of List.binarySearchBy()

The syntax of List.binarySearchBy() extension function is:

fun <T, K : Comparable<K>> List<T>.binarySearchBy( key: K?, fromIndex: Int = 0, toIndex: Int = size, selector: (T) -> K? ): Int

This binarySearchBy() extension function of List searches this list or its range for an element having the key returned by the specified selector function equal to the provided key value using the binary search algorithm. The list is expected to be sorted into ascending order according to the Comparable natural ordering of keys of its elements. otherwise the result is undefined.



✐ Examples

1 Example

In this example,

  • We create a list named list containing integers from 1 to 6.
  • We use the binarySearchBy function to find the index of an element with key 4 in list.
  • The selector function { it } returns the element itself as the key.
  • The result, which is the index of the element with key 4 in list, is stored in index.
  • We print the index of the element with key 4 using println.

Kotlin Program

fun main(args: Array<String>) {
    val list = listOf(1, 2, 3, 4, 5, 6)
    val index = list.binarySearchBy(4) { it }
    println("Index of element with key 4: \$index")
}

Output

Index of element with key 4: 3

2 Example

In this example,

  • We create a list named list containing strings "apple", "banana", "cherry", "grape".
  • We use the binarySearchBy function to find the index of an element with key "cherry" in list.
  • The selector function { it } returns the element itself as the key.
  • The result, which is the index of the element with key "cherry" in list, is stored in index.
  • We print the index of the element with key "cherry" using println.

Kotlin Program

fun main(args: Array<String>) {
    val list = listOf("apple", "banana", "cherry", "grape")
    val index = list.binarySearchBy("cherry") { it }
    println("Index of element with key 'cherry': \$index")
}

Output

Index of element with key 'cherry': 2

3 Example

In this example,

  • We define a data class Person with properties name and age.
  • We create a list named list containing instances of Person.
  • We use the binarySearchBy function to find the index of an element with key 25 in list, where the key is the age property of each Person instance.
  • The result, which is the index of the element with age 25 in list, is stored in index.
  • We print the index of the element with age 25 using println.

Kotlin Program

fun main(args: Array<String>) {
    data class Person(val name: String, val age: Int)
    val list = listOf(Person("Alice", 30), Person("Bob", 25), Person("Charlie", 35))
    val index = list.binarySearchBy(25) { it.age }
    println("Index of element with age 25: \$index")
}

Output

Index of element with age 25: 1

Summary

In this Kotlin tutorial, we learned about binarySearchBy() extension function of List: the syntax and few working examples with output and detailed explanation for each example.