Ordering<T> class abstract

An ordering implements a Comparator function that can be modified using a fluent interface.

The Ordering defines a total order on objects of type T. For example, the natural order of two String instances can be determined using:

final natural = Ordering.natural<String>();
natural.compare('a', 'b');  // -1

However, the low-level compare function rarely needs to be used directly. Ordering implements various fluent helpers that create new orderings

natural.reverse();  // a reverse ordering
natural.nullsFirst();  // orders null values before other values
natural.compound(other);  // breaks ties of natural with other order

and that allow the user to perform common tasks on an ordering:

natural.min('a', 'b');  // 'a'
natural.maxOf(['a', 'b', 'c']);  // 'c'
natural.sorted(['dog', 'cat', 'ape']);  // ['ape', 'cat', 'dog']
natural.isOrdered(['ape', 'cat', 'dog']);  // true
Mixed in types
Annotations
  • @immutable
  • @Deprecated("Use the extension methods on the built-in `Comparator` instead")

Constructors

Ordering()
Internal default constructor for subclasses.
const
Ordering.explicit(Iterable<T> iterable)
Returns an explicit ordering based on an iterable of elements.
factory
Ordering.from(Comparator<T> comparator)
Returns an ordering based on a comparator function.
factory
Ordering.of(Comparator<T> comparator)
Returns an ordering based on a comparator function.
factory

Properties

defaultToStringPrinter ObjectPrinter
Override to configure the empty ObjectPrinter.
no setterinherited
hashCode int
The hash code for this object.
no setterinherited
lexicographical Ordering<Iterable<T>>
Returns an ordering that orders iterables lexicographically by their elements.
no setter
nullsFirst Ordering<T?>
Returns an ordering that orders null values before non-null values.
no setter
nullsLast Ordering<T?>
Returns an ordering that orders null values after non-null values.
no setter
reversed Ordering<T>
Returns the reversed ordering.
no setter
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
toStringPrinter ObjectPrinter
Override and call super to add values to the ObjectPrinter.
no setterinherited

Methods

binarySearch(List<T> list, T value, {int? start, int? end}) int
Searches the sorted list for the specified value using binary search.
compare(T a, T b) int
Compares two objects a and b with each other and returns
compound(Ordering<T> other) Ordering<T>
Returns an ordering that breaks the tie of the receiver by using other.
equalTo(T a) Predicate1<T>
Creates a predicate that evaluates to true for values equal to a.
greaterThan(T a) Predicate1<T>
Creates a predicate that evaluates to true for values more than a.
greaterThanOrEqualTo(T a) Predicate1<T>
Creates a predicate that evaluates to true for values at least a.
isOrdered(Iterable<T> iterable) bool
Tests if the specified iterable is in increasing order.
isStrictlyOrdered(Iterable<T> iterable) bool
Tests if the specified Iterable is in strict increasing order.
lessThan(T a) Predicate1<T>
Creates a predicate that evaluates to true for values smaller than a.
lessThanOrEqualTo(T a) Predicate1<T>
Creates a predicate that evaluates to true for values at most a.
max(T a, T b) → T
Returns the maximum of the two arguments a and b.
maxOf(Iterable<T> iterable, {T orElse()?}) → T
Returns the maximum of the provided iterable.
min(T a, T b) → T
Returns the minimum of the two arguments a and b.
minOf(Iterable<T> iterable, {T orElse()?}) → T
Returns the minimum of the provided iterable.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
notEqualTo(T a) Predicate1<T>
Creates a predicate that evaluates to true for values equal to a.
onResultOf<F>(T function(F argument)) Ordering<F>
Returns an ordering that uses the provided function to transform the result.
percentile(Iterable<T> iterable, num percentile, {bool isOrdered = false, T orElse()?}) → T
Returns the value of the iterable at the given percentile between 0.0 and 1.0. Throws a RangeError if percentile is outside that range. To return the median, use a percentile of 0.5.
sort(List<T> list) → void
Sorts the provided list in-place.
sorted(Iterable<T> iterable) List<T>
Returns a sorted copy of the provided iterable.
toString() String
Standard toString implementation. Do not override, instead implement toStringPrinter to customize.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited

Static Methods

natural<T extends Comparable<Object?>>() Ordering<T>
Returns a natural ordering of objects.