Skip to content

Instantly share code, notes, and snippets.

@nmwilk
Created June 2, 2020 09:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save nmwilk/68ae0424e848b9f05a8239db6b708390 to your computer and use it in GitHub Desktop.
Save nmwilk/68ae0424e848b9f05a8239db6b708390 to your computer and use it in GitHub Desktop.
Easy sorting of maps by value
/// Usage
/// foos.sortedBy((it) => it.bar);
extension MapExt<T, U> on Map<T, U> {
Map<T, U> sortedBy(Comparable value(U u)) {
final entries = this.entries.toList();
entries.sort((a, b) => value(a.value).compareTo(value(b.value)));
return Map<T, U>.fromEntries(entries);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment