Created
June 2, 2020 09:20
-
-
Save nmwilk/68ae0424e848b9f05a8239db6b708390 to your computer and use it in GitHub Desktop.
Easy sorting of maps by value
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// 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