compareTo method

  1. @override
int compareTo(
  1. InstanceMirror other
)
override

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.

Implementation

@override
int compareTo(InstanceMirror other) {
  final fieldMirrors = this.fieldMirrors.toList();
  final otherFieldMirrors = other.fieldMirrors.toList();
  final length = min<int>(fieldMirrors.length, otherFieldMirrors.length);
  for (var i = 0; i < length; i++) {
    final fieldMirror = fieldMirrors[i];
    final otherFieldMirror = otherFieldMirrors[i];
    if (fieldMirror.traits.contains(Trait.noEquality)) {
      return -1;
    }
    if (otherFieldMirror.traits.contains(Trait.noEquality)) {
      return 1;
    }
    {
      final r = fieldMirror.name.compareTo(otherFieldMirror.name);
      if (r != 0) {
        return r;
      }
    }
    final fieldValue = get(fieldMirror.name);
    final otherFieldValue = get(otherFieldMirror.name);
    final r = fieldMirror.kind.compare(fieldValue, otherFieldValue);
    if (r != 0) {
      return 0;
    }
  }
  return 0;
}