compareByIndex<T extends Enum> 静态方法
- @Since("2.15")
- T value1,
- T value2
通过指数比较两个枚举值。
为枚举类型定义的泛型 Comparator 函数,该函数根据枚举值的 指数 排序,这与在 enum
声明中枚举元素声明的源顺序相对应。
示例
enum Season { spring, summer, autumn, winter }
void main() {
var relationByIndex =
Enum.compareByIndex(Season.spring, Season.summer); // < 0
relationByIndex =
Enum.compareByIndex(Season.summer, Season.spring); // > 0
relationByIndex =
Enum.compareByIndex(Season.spring, Season.winter); // < 0
relationByIndex =
Enum.compareByIndex(Season.winter, Season.spring); // > 0
}
实现
@Since("2.15")
static int compareByIndex<T extends Enum>(T value1, T value2) =>
value1.index - value2.index;