compareTo method Null safety

  1. @override
int compareTo(
  1. SemanticsSortKey other

Compares this object to another object.

Returns a value like a Comparator when comparing this to other. That is, it returns a negative integer if this is ordered before other, a positive integer if this is ordered after other, and zero if this and other are ordered together.

The other argument must be a value that is comparable to this object.


int compareTo(SemanticsSortKey other) {
  // Sort by name first and then subclass ordering.
  assert(runtimeType == other.runtimeType, 'Semantics sort keys can only be compared to other sort keys of the same type.');

  // Defer to the subclass implementation for ordering only if the names are
  // identical (or both null).
  if (name == {
    return doCompare(other);

  // Keys that don't have a name are sorted together and come before those with
  // a name.
  if (name == null && != null) {
    return -1;
  } else if (name != null && == null) {
    return 1;

  return name!.compareTo(!);