ordered_set 1.1.4 copy "ordered_set: ^1.1.4" to clipboard
ordered_set: ^1.1.4 copied to clipboard

outdated

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);
}
32
likes
0
pub points
91%
popularity

Publisher

verified publisherblue-fire.xyz

A simple implementation of an Ordered Set for Dart that allows multiple items with the same priority.

Repository (GitHub)
View/report issues

License

unknown (license)

More

Packages that depend on ordered_set