compareByName<T extends Enum> static method

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

Compares enum values by name.

The EnumName.name of an enum value is a string representing the source name used to declare that enum value.

This Comparator compares two enum values by comparing their names, and can be used to sort enum values by their names. The comparison uses String.compareTo, and is therefore case sensitive.

Example:

enum Season { spring, summer, autumn, winter }

void main() {
  var seasons = [...Season.values]..sort(Enum.compareByName);
  print(seasons);
  // [Season.autumn, Season.spring, Season.summer, Season.winter]
}

Implementation

@Since("2.15")
static int compareByName<T extends Enum>(T value1, T value2) =>
    value1.name.compareTo(value2.name);