-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Closed as not planned
Closed as not planned
Copy link
Description
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
enum SortableType { A, B, C }
void main() {
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
enumby itsindexis 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?
Environment Details
Any.
Dartpad: https://dartpad.dev/
julemand101
Metadata
Metadata
Assignees
Labels
No labels