filterBy
Filter function used to query the available items based on the user's input.
By default, the toString representation of each item is used to find items matching the current query. In most cases, however, a specific field (e.g. a country's name) is needed in order to get meaningful results!
The filterBy
property takes any function with the following signature: (Sequence<Country>, String) -> Sequence<Country>
.
Since in practice many filterings rely on a single String
property, a getter function returning a String
(T.() -> String
) can be passed as well.
Important: Using non-String items and not providing a specialized filter function may result in undefined behavior regarding the filter results! This is due to default implementation using
toString()
to find matching items for each query. If you still need to use the default filter function it can explicitly be set via filterBy.default.
Important: The
filterBy
function handles the un-shortened, full amount of available items and is highly time-critical! Try to use efficient Sequence-based operations whenever possible. Functions like fold or sortedBy do still iterate over the whole Sequence internally, which is why they should be used with caution here.
Example:
data class Country(val code: String, val name: String)
// ...
filterBy(Country::name)
// OR
filterBy { countries, query ->
countries.filter { country ->
country.name.contains(query, ignoreCase = true)
}
}