Kotlin compare values by

compareValuesBy

Compares two values using the specified functions selectors to calculate the result of the comparison. The functions are called sequentially, receive the given values a and b and return Comparable objects. As soon as the Comparable instances returned by a function for a and b values do not compare as equal, the result of that comparison is returned.

import kotlin.test.* fun main(args: Array) < //sampleStart fun compareLengthThenString(a: String, b: String): Int = compareValuesBy(a, b, < it.length >, < it >) println("compareLengthThenString("b", "aa") < 0 is $") // true println("compareLengthThenString("a", "b") < 0 is $") // true println("compareLengthThenString("b", "a") > 0 is $ 0>") // true println("compareLengthThenString("a", "a") == 0 is $") // true //sampleEnd >
inline fun compareValuesBy( a: T, b: T, selector: (T) -> Comparable? ): Int

Compares two values using the specified selector function to calculate the result of the comparison. The function is applied to the given values a and b and return Comparable objects. The result of comparison of these Comparable instances is returned.

import kotlin.test.* fun main(args: Array) < //sampleStart fun compareLength(a: String, b: String): Int = compareValuesBy(a, b) < it.length >println("compareLength("a", "b") == 0 is $") // true println("compareLength("bb", "a") > 0 is $ 0>") // true println("compareLength("a", "bb") < 0 is $") // true //sampleEnd >
inline fun compareValuesBy( a: T, b: T, comparator: Comparator, selector: (T) -> K ): Int

Compares two values using the specified selector function to calculate the result of the comparison. The function is applied to the given values a and b and return objects of type K which are then being compared with the given comparator.

import kotlin.test.* fun main(args: Array) < //sampleStart fun compareInsensitiveOrder(a: Char, b: Char): Int = compareValuesBy(a, b, String.CASE_INSENSITIVE_ORDER, < c ->c.toString() >) println("compareInsensitiveOrder('a', 'a') == 0 is $") // true println("compareInsensitiveOrder('a', 'A') == 0 is $") // true println("compareInsensitiveOrder('a', 'b') < 0 is $") // true println("compareInsensitiveOrder('A', 'b') < 0 is $") // true println("compareInsensitiveOrder('b', 'a') > 0 is $ 0>") // true //sampleEnd >

Источник

compareBy

Creates a comparator using the sequence of functions to calculate a result of comparison. The functions are called sequentially, receive the given values a and b and return Comparable objects. As soon as the Comparable instances returned by a function for a and b values do not compare as equal, the result of that comparison is returned from the Comparator.

import kotlin.test.* fun main(args: Array) < //sampleStart val list = listOf("aa", "b", "bb", "a") val sorted = list.sortedWith(compareBy( < it.length >, < it >)) println(sorted) // [a, b, aa, bb] //sampleEnd >

Creates a comparator using the function to transform value to a Comparable instance for comparison.

import kotlin.test.* fun main(args: Array) < //sampleStart val list = listOf("aa", "b", "bb", "a") val sorted = list.sortedWith(compareBy < it.length >) println(sorted) // [b, a, aa, bb] //sampleEnd >

inline fun < T , K >compareBy (
comparator : Comparator < in K >,
crossinline selector : ( T ) -> K
) : Comparator < T >
(source)

Creates a comparator using the selector function to transform values being compared and then applying the specified comparator to compare transformed values.

import kotlin.test.* fun main(args: Array) < //sampleStart val list = listOf('B', 'a', 'A', 'b') val sorted = list.sortedWith( compareBy(String.CASE_INSENSITIVE_ORDER) < v ->v.toString() > ) println(sorted) // [a, A, B, b] //sampleEnd >

Источник

Kotlin compare values by

Helper functions for creating Comparator instances.

Helper functions for creating Comparator instances.

Functions

compareBy

Creates a comparator using the sequence of functions to calculate a result of comparison. The functions are called sequentially, receive the given values a and b and return Comparable objects. As soon as the Comparable instances returned by a function for a and b values do not compare as equal, the result of that comparison is returned from the Comparator.

Creates a comparator using the function to transform value to a Comparable instance for comparison.

Creates a comparator using the selector function to transform values being compared and then applying the specified comparator to compare transformed values.

compareByDescending

Creates a descending comparator using the function to transform value to a Comparable instance for comparison.

Creates a descending comparator using the selector function to transform values being compared and then applying the specified comparator to compare transformed values.

compareValues

Compares two nullable Comparable values. Null is considered less than any value.

compareValuesBy

Compares two values using the specified functions selectors to calculate the result of the comparison. The functions are called sequentially, receive the given values a and b and return Comparable objects. As soon as the Comparable instances returned by a function for a and b values do not compare as equal, the result of that comparison is returned.

Compares two values using the specified selector function to calculate the result of the comparison. The function is applied to the given values a and b and return Comparable objects. The result of comparison of these Comparable instances is returned.

Compares two values using the specified selector function to calculate the result of the comparison. The function is applied to the given values a and b and return objects of type K which are then being compared with the given comparator.

Источник

compareValuesBy

Compares two values using the specified functions selectors to calculate the result of the comparison. The functions are called sequentially, receive the given values a and b and return Comparable objects. As soon as the Comparable instances returned by a function for a and b values do not compare as equal, the result of that comparison is returned.

import kotlin.test.* fun main(args: Array) < //sampleStart fun compareLengthThenString(a: String, b: String): Int = compareValuesBy(a, b, < it.length >, < it >) println("compareLengthThenString(\"b\", \"aa\") < 0 is $") // true println("compareLengthThenString(\"a\", \"b\") < 0 is $") // true println("compareLengthThenString(\"b\", \"a\") > 0 is $ 0>") // true println("compareLengthThenString(\"a\", \"a\") == 0 is $") // true //sampleEnd >

Compares two values using the specified selector function to calculate the result of the comparison. The function is applied to the given values a and b and return Comparable objects. The result of comparison of these Comparable instances is returned.

import kotlin.test.* fun main(args: Array) < //sampleStart fun compareLength(a: String, b: String): Int = compareValuesBy(a, b) < it.length >println("compareLength(\"a\", \"b\") == 0 is $") // true println("compareLength(\"bb\", \"a\") > 0 is $ 0>") // true println("compareLength(\"a\", \"bb\") < 0 is $") // true //sampleEnd >

inline fun < T , K >compareValuesBy (
a : T ,
b : T ,
comparator : Comparator < in K >,
selector : ( T ) -> K
) : Int
(source)

Compares two values using the specified selector function to calculate the result of the comparison. The function is applied to the given values a and b and return objects of type K which are then being compared with the given comparator.

import kotlin.test.* fun main(args: Array) < //sampleStart fun compareInsensitiveOrder(a: Char, b: Char): Int = compareValuesBy(a, b, String.CASE_INSENSITIVE_ORDER, < c ->c.toString() >) println("compareInsensitiveOrder('a', 'a') == 0 is $") // true println("compareInsensitiveOrder('a', 'A') == 0 is $") // true println("compareInsensitiveOrder('a', 'b') < 0 is $") // true println("compareInsensitiveOrder('A', 'b') < 0 is $") // true println("compareInsensitiveOrder('b', 'a') > 0 is $ 0>") // true //sampleEnd >

Источник

compareValuesBy

Compares two values using the specified functions selectors to calculate the result of the comparison. The functions are called sequentially, receive the given values a and b and return Comparable objects. As soon as the Comparable instances returned by a function for a and b values do not compare as equal, the result of that comparison is returned.

import kotlin.test.* fun main(args: ArrayString>) < //sampleStart fun compareLengthThenString(a: String, b: String): Int = compareValuesBy(a, b, < it.length >, < it >) println("compareLengthThenString(\"b\", \"aa\") < 0 is $"b", "aa") < 0>") // true println("compareLengthThenString(\"a\", \"b\") < 0 is $"a", "b") < 0>") // true println("compareLengthThenString(\"b\", \"a\") > 0 is $"b", "a") > 0>") // true println("compareLengthThenString(\"a\", \"a\") == 0 is $"a", "a") == 0>") // true //sampleEnd >
inline fun compareValuesBy( a: T, b: T, selector: (T) -> Comparable? ): Int

Compares two values using the specified selector function to calculate the result of the comparison. The function is applied to the given values a and b and return Comparable objects. The result of comparison of these Comparable instances is returned.

import kotlin.test.* fun main(args: ArrayString>) < //sampleStart fun compareLength(a: String, b: String): Int = compareValuesBy(a, b) < it.length >println("compareLength(\"a\", \"b\") == 0 is $"a", "b") == 0>") // true println("compareLength(\"bb\", \"a\") > 0 is $"bb", "a") > 0>") // true println("compareLength(\"a\", \"bb\") < 0 is $"a", "bb") < 0>") // true //sampleEnd >
inline fun compareValuesBy( a: T, b: T, comparator: Comparatorin K>, selector: (T) -> K ): Int

Compares two values using the specified selector function to calculate the result of the comparison. The function is applied to the given values a and b and return objects of type K which are then being compared with the given comparator.

import kotlin.test.* fun main(args: ArrayString>) < //sampleStart fun compareInsensitiveOrder(a: Char, b: Char): Int = compareValuesBy(a, b, String.CASE_INSENSITIVE_ORDER, < c ->c.toString() >) println("compareInsensitiveOrder('a', 'a') == 0 is $'a', 'a') == 0>") // true println("compareInsensitiveOrder('a', 'A') == 0 is $'a', 'A') == 0>") // true println("compareInsensitiveOrder('a', 'b') < 0 is $'a', 'b') < 0>") // true println("compareInsensitiveOrder('A', 'b') < 0 is $'A', 'b') < 0>") // true println("compareInsensitiveOrder('b', 'a') > 0 is $'b', 'a') > 0>") // true //sampleEnd >

Источник

Читайте также:  Encode html codes php
Оцените статью