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 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.


enum Season { spring, summer, autumn, winter }

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


static int compareByName<T extends Enum>(T value1, T value2) =>;