ordered_set 1.1.4 ordered_set: ^1.1.4 copied to clipboard
A simple implementation of an Ordered Set for Dart that allows multiple items with the same priority.
example/ordered_set_example.dart
import 'package:ordered_set/ordered_set.dart';
import 'package:ordered_set/comparing.dart';
main() {
OrderedSet<int> items = OrderedSet();
items.add(2);
items.add(1);
print(items.toList()); // [1, 2]
OrderedSet<Person> a = OrderedSet((a, b) => a.age - b.age);
a.add(Person(12, 'Klaus'));
a.add(Person(1, 'Sunny'));
a.add(Person(14, 'Violet'));
print(a.elementAt(0).name); // Sunny
print(a.elementAt(2).name); // Violet
a.add(Person(13, 'Isadora'));
a.add(Person(13, 'Duncan'));
a.add(Person(13, 'Quigley'));
print(a.toList().map((e) => e.name));
// Sunny, Klaus, Isadora, Duncan, Quigley, Violet
// use Comparing for simpler creation:
// sort by age desc and then name asc
OrderedSet<Person> b =
OrderedSet(Comparing.join([(p) => -p.age, (p) => p.name]));
b.addAll(a.toList());
print(b.toList().map((e) => e.name));
// Violet, Duncan, Isadora, Quigley, Klaus, Sunny
}
class Person {
int age;
String name;
Person(this.age, this.name);
}