In order to assist the creation of Comparators, the Comparing class can be used:
// sort by name lengthfinal people = OrderedSet.comparing<Person>(Comparing.on((p) => p.name.length));
// sort by name descfinal people = OrderedSet.comparing<Person>(Comparing.reverse(Comparing.on((p) => p.name)));
// sort by role and then by namefinal people = OrderedSet.comparing<Person>(Comparing.join([(p) => p.role, (p) => p.name]));
Note that you could instead just create a MappingOrderedSet instead:
final people = OrderedSet.mapping<num, Person>((p) => p.name.length);
// ...
Mapping vs Comparing
There are two main implementations of the OrderedSet interface:
ComparingOrderedSet: the simplest implementation, takes in a Comparator and does not cache
priorities. It uses Dart's SplayTreeSet as a backing implementation.
MappingOrderedSet: a slightly more advanced implementation that takes in a mapper function
(maps elements to their priorities) and caches them. It uses Dart's SplayTreeMap as a backing
implementation.
In order to create an OrderedSet, however, you can just use the static methods on the interface
itself:
OrderedSet.comparing<E>([comparator]): creates a ComparingOrderedSet with the given
Comparator.
OrderedSet.mapping<K, E>([mapper]): creates a MappingOrderedSet with the given mapper
function.
OrderedSet.comparable<K, E>(): if E extends Comparable<K>, this is a simpler way of creating
a MappingOrderedSet with identity mapping.
OrderedSet.simple<E>(): if E extends Comparable<E>, this is an even simpler way of creating
a MappingOrderedSet with identity mapping.
Querying
You can [register] a set of queries, i.e., predefined sub-types, whose results, i.e., subsets of
this set, are then cached. Since the queries have to be type checks, and types are runtime
constants, this can be vastly optimized.
You can then filter by type by using the [query] method (or using [whereType]; which is overridden).
Note that you can change [strictMode] to allow for querying for unregistered types; if you do so,
the registration cost is payed on the first query.
Contributing
All contributions are very welcome! Please feel free to create Issues, help us with PR's or comment
your suggestions, feature requests, bugs, et cetera. Give us a star if you liked it!