摘要: 本文介绍 Kotlin 的排序的高级用法,实现对于对象灵活的排序,基本可以覆盖所有对于对象列表的排序需求。
介绍
这里的高级排序主要是依赖 sortWith
这个函数是 Array 的 Extention 函数,Kotlin 的标准库还提供了一个 Collection 的版本封装sortedWith
,用法一致,因为在源码上看最终调用的就是 Array 的函数,但是后一个在我们处理 Collection 的时候更加方便。
但是有一个有点不太理解无法找到 该函数的最终源码,因为这个方法好像是通过生成器生成的,所以在源码中找不到对应的实现,尝试搜索了半天无果,可能这个实现是依赖一个现有的 JDK 方法。
1 | /** |
函数的用法很简单,就是传入一个 Comparator
,针对对象进行比较。
这里我们主要介绍一下这个 Comparator
针对不同场景的使用方法。
案例
1 | products.sortWith(object : Comparator<Product> { |
Comparator 返回值总结
我们如何理解这个排序的正反序列呢?
首先我们明确参数意义
- p1 后一个数据
- p2 前一个数据
然后返回值的意义
- 返回 1 保持 p1 ,p2 相对位置关系
- 返回 -1 需要交换 p1, p2
- 返回 0 不关心两者关系,保持现状
所以我们可以看到,如果我们需要
- 升序排序(自然序列) asceding,也就是后一个比前一个大就保持相对位置,也就是返回 1.
- 降序排序 descending, 反之后一个比前一个大,就要交换位置,返回 -1
我们进一步总结
a1 > a2 -> 1
a1 < a2 -> -1
就是升序排序,后者大于前者,就保持。
当然我么这里除了比较大小外,可以对于属性的大小约束,不管我们的条件是什么,只需要最终决定返回 1或者-1,就可以知道排序。
比如我们有个属性是 Boolean, 我们规定 true 排在 false 前面,我们同样可以使用类似的方法,翻译成我们的规则就是
- 如果后者是 true 前者是 false,就是交换 -1
- 如果后者是 false 前者是 true,就要保持相对顺序,返回1
Comparator 用于 Max 操作
1 | val maxSupport = supportedSemverList.maxWith(Comparator { p1, p2 -> |
参考
https://grokonez.com/kotlin/kotlin-array-sort-sortby-sortwith
public interface Comparator
A comparison function, which imposes a total ordering on some collection of objects.
- Comparators can be passed to a sort method (such as Collections.sort or Arrays.sort) to allow precise control over the sort order.
- Comparators can also be used to control the order of certain data structures (such as sorted sets or sorted maps), or to provide an ordering for collections of objects that don’t have a natural ordering.
The ordering imposed by a comparator c on a set of elements S is said to be consistent with equals if and only if c.compare(e1, e2)==0 has the same boolean value as e1.equals(e2) for every e1 and e2 in S.
Caution should be exercised when using a comparator capable of imposing an ordering inconsistent with equals to order a sorted set (or sorted map). Suppose a sorted set (or sorted map) with an explicit comparator c is used with elements (or keys) drawn from a set S. If the ordering imposed by c on S is inconsistent with equals, the sorted set (or sorted map) will behave “strangely.” In particular the sorted set (or sorted map) will violate the general contract for set (or map), which is defined in terms of equals.
For example, suppose one adds two elements a and b such that (a.equals(b) && c.compare(a, b) != 0) to an empty TreeSet with comparator c. The second add operation will return true (and the size of the tree set will increase) because a and b are not equivalent from the tree set’s perspective, even though this is contrary to the specification of the Set.add method.
Note: It is generally a good idea for comparators to also implement java.io.Serializable, as they may be used as ordering methods in serializable data structures (like TreeSet, TreeMap). In order for the data structure to serialize successfully, the comparator (if provided) must implement Serializable.
For the mathematically inclined, the relation that defines the imposed ordering that a given comparator c imposes on a given set of objects S is:
{(x, y) such that c.compare(x, y) <= 0}.
The quotient for this total order is:
{(x, y) such that c.compare(x, y) == 0}.
It follows immediately from the contract for compare that the quotient is an equivalence relation on S, and that the imposed ordering is a total order on S. When we say that the ordering imposed by c on S is consistent with equals, we mean that the quotient for the ordering is the equivalence relation defined by the objects’ equals(Object) method(s):
{(x, y) such that x.equals(y)}.
Unlike Comparable, a comparator may optionally permit comparison of null arguments, while maintaining the requirements for an equivalence relation.
This interface is a member of the Java Collections Framework.
Since:
1.2
See Also:
Comparable, Serializable
Type parameters: