compareByIndex<T extends Enum> static method

  1. @Since("2.15")
int compareByIndex<T extends Enum>(
  1. T value1,
  2. T value2
)

Compares two enum values by their index.

A generic Comparator function for enum types which orders enum values by their index value, which corresponds to the source order of the enum element declarations in the enum declaration.

Example:

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
}

Implementation

@Since("2.15")
static int compareByIndex<T extends Enum>(T value1, T value2) =>
    value1.index - value2.index;