function - kotlin 中 sortArrayWith 的源代码在哪里?
问题描述
我正在阅读这段代码并试图弄清楚 sorWith 如何使用比较器对象。函数定义如下:
public actual fun <T> Array<out T>.sortWith(comparator: Comparator<in T>): Unit {
if (size > 1) sortArrayWith(this, 0, size, comparator)}
但是我无法在sortArrayWith
任何地方找到源代码。我想知道它实际上是如何使用的comparator
这是上面提到的代码:
val products = arrayOf(Product("iPhone 8 Plus 64G", 850.00),
Product("iPhone 8 Plus 256G", 1100.00),
Product("Apple iPod touch 16GB", 246.00),
Product("Apple iPod Nano 16GB", 234.75),
Product("iPad Pro 9.7-inch 32 GB", 474.98),
Product("iPad Pro 9.7-inch 128G", 574.99),
Product("Apple 42mm Smart Watch", 284.93))
// public fun Array.sortWith(comparator: Comparator): Unit
// -> Sorts the array in-place according to the order specified by the given [comparator].
products.sortWith(object: Comparator{
override fun compare(p1: Product, p2: Product): Int = when {
p1.price > p2.price -> 1
p1.price == p2.price -> 0
else -> -1
}
})
解决方案
推荐阅读
- r - 避免在 glmnet::cv.glmnet 中计算截距
- file-conversion - 关于 MaxTract(将 pdf 转换为 LaTeX 的软件)的一些问题
- java - 在 Spring Boot 中,如何在 @WebMvcTest 期间从启动器启用 @ControllerAdvice
- vba - VBA 用于从老化报告中制作动态的桶式报告,同时从表单中提供天数
- nsis - 使用 NSIS,自定义页面两个问题。ListView / NSISList 文本
- github - GitHub页面项目站点的自定义域?
- ecmascript-6 - setTimout 函数在异步函数中返回未定义
- internet-explorer - 代理设置不适用于 Internet Explorer 10 或 11 (Windows 10)
- python - 搜索并提取以特定字符串开头和结尾的特定字符串
- reactjs - 使用 mapdispatchprops 删除多个数据在反应中不起作用