3

Kotlin 复合排序语法糖/多字段排序语法糖

 2 years ago
source link: http://i.lckiss.com/?p=7524
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Kotlin 复合排序语法糖/多字段排序语法糖

2021-10-02

平时其实很少需要这样的条件,但偶尔还是会遇到,例如:一个股池,先按板块类型排序,再在这个基础上按其跌涨幅排序。Java 中有现成的 compare thenComparing,但 kotlin 中好像怎么点都点不出来,找了找语法糖,于是找到了这个:

        arrayListOf<Stock>()
            .sortedWith(compareByDescending<Stock> { it.type }
                .thenByDescending { it.upDown })

拿来即用。

参考:https://stackoverflow.com/questions/369512/how-to-compare-objects-by-multiple-fields


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK