You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Enum .sort() Throws an Error Unless Explicit Comparator is Provided
Description
I encountered an inconsistency when sorting a list of enums in Dart. Calling .sort() directly on a List<Enum> throws an error, but providing an explicit comparator using .index.compareTo() works as expected.
Steps to Reproduce
enumSortableType { A, B, C }
voidmain() {
var list1 =SortableType.values.toList();
var list2 =SortableType.values.toList();
list1.sort(); // Throws an error
list2.sort((a, b) => a.index.compareTo(b.index)); // Works fine
}
Expected Behavior
Since sorting an enum by its index is the most logical behavior, list1.sort(); should work without requiring a custom comparator.
Actual Behavior
list1.sort(); throws an error.
list2.sort((a, b) => a.index.compareTo(b.index)); works fine.
Proposal
Would it make sense for Enum to implement Comparable by default using its index?