ConvertingComparator<S, T> class

A comparator that compares source values of type S by first converting them to another type T using a Converter, then applying a Comparator on T.

This is useful when you want to sort values based on a derived property or a type transformation.


πŸ”§ Example: Compare strings by their lengths

final comparator = ConvertingComparator<String, int>(
  Comparator.naturalOrder(),
  Converter<String, int>((s) => s.length),
);

final list = ['pear', 'banana', 'kiwi'];
list.sort(comparator); // ['kiwi', 'pear', 'banana']

You can also use the factory constructors:

πŸ”§ Using withConverter and a ConversionService:

final service = DefaultConversionService();
final comparator = ConvertingComparator.withConverter<String, int>(
  Comparator.naturalOrder(),
  service,
  Class<int>(),
);
Annotations
  • @Generic.new(ConvertingComparator)

Constructors

ConvertingComparator(Comparator<T> _comparator, Converter<S, T> _converter)
A comparator that compares source values of type S by first converting them to another type T using a Converter, then applying a Comparator on T.
ConvertingComparator.converter(Converter<S, T> converter)
A comparator that compares source values of type S by first converting them to another type T using a Converter, then applying a Comparator on T.
ConvertingComparator.withConverter(Comparator<T> comparator, ConversionService conversionService, Class<T> targetType)
A comparator that compares source values of type S by first converting them to another type T using a Converter, then applying a Comparator on T.

Properties

hashCode β†’ int
The hash code for this object.
no setterinherited
runtimeType β†’ Type
A representation of the runtime type of the object.
no setterinherited

Methods

compare(S a, S b) β†’ int
Compares two values of type T for order.
noSuchMethod(Invocation invocation) β†’ dynamic
Invoked when a nonexistent method or property is accessed.
inherited
reversed() β†’ Comparator<S>
Returns a comparator that imposes the reverse ordering of this comparator.
inherited
thenComparing(Comparator<S> other) β†’ Comparator<S>
Chains another comparator to be used if this comparator returns 0 (equality).
inherited
thenComparingComparable<U extends Comparable<U>>(U keyExtractor(S)) β†’ Comparator<S>
Chains a comparator by extracting a Comparable key from each element.
inherited
thenComparingWith<U>(U keyExtractor(S), Comparator<U> keyComparator) β†’ Comparator<S>
Chains a comparator by extracting a key with keyExtractor and comparing it using the given keyComparator.
inherited
toString() β†’ String
A string representation of this object.
inherited

Operators

operator ==(Object other) β†’ bool
The equality operator.
inherited

Static Methods

mapEntryKeys<K, V>(Comparator<K> comparator) β†’ ConvertingComparator<MapEntry<K, V>, K>
Creates a comparator that compares MapEntry objects by their keys.
mapEntryValues<K, V>(Comparator<V> comparator) β†’ ConvertingComparator<MapEntry<K, V>, V>
Creates a comparator that compares MapEntry objects by their values.