EqualityComparer<T> class

Used in various enumeration methods for allowing user-defined comparisons between complex elements.

EqualityComparer exposes three different forms of comparison - equivalency, ordering, and hashing. Any of the options can be omitted when creating an EqualityComparer instance, in which case the omitted options will revert to default behavior as outlined below.

The EqualityComparer.compare field tests equivalency. Two elements of a given type are passed to the function, and the function returns whether or not the elements are deemed equal. If omitted, the function will default to checking for strict equivalency by using the == operator.

(left, right) { return left == right; }

The EqualityComparer.hash field generates hash codes. An element is passed to the function and the function returns an integer value that represents the element's hash code. If omitted, the function will default to calling the object's hashCode property:

(value) { return value.hashCode; }

The EqualityComparer.sort field tests for sorting order. Two elements of a given type are passed to the function, and the function returns an integer that represents the sorting order of the elements. If the left value is lesser than the right value, the returned integer is negative. If the left value is greater than the right value, the returned integer is positive. If the two values are equal, 0 is returned. (This behavior is identical to various compareTo methods on Dart types.)

If the EqualityComparer.sort field is omitted, the function's default behavior depends on the type argument passed to EqualityComparer:

  • int will default to using int.compareTo
  • num will default to using num.compareTo
  • double will default to using double.compareTo
  • String will default to using String.compareTo
  • All other types will default to a non-sorting function:
(left, right) { return 0; }

Constructors

EqualityComparer({Comparer<T> comparer, Hasher<T> hasher, Sorter<T> sorter })

Properties

compare Comparer<T>
final
hash Hasher<T>
final
sort Sorter<T>
final
hashCode → int
The hash code for this object. [...]
read-only, inherited
runtimeType → Type
A representation of the runtime type of the object.
read-only, inherited

Methods

noSuchMethod(Invocation invocation) → dynamic
Invoked when a non-existent method or property is accessed. [...]
inherited
toString() → String
Returns a string representation of this object.
inherited

Operators

operator ==(dynamic other) → bool
The equality operator. [...]
inherited

Static Methods

forType<T>() EqualityComparer<T>