Instead of using the compare
in sort
every time, you can override a method compareTo
in your object.
void main() {
final sortedArray = <A>[A(3), A(1), A(2)]..sort();
print(sortedArray); // <A>[A(1), A(2), A(3)]
}
class A with Comparabte<A> {
const A(this.id);
final int id;
@override
int compareTo(A other) => id.compareTo(other.id);
@override
String toString() => 'A($id)';
}