ranked function

List<MapEntry<String, int>> ranked(
  1. Map<String, int> counts, {
  2. required Set<String> scope,
  3. int top = 10,
})

The top highest counts among the files in scope, highest first and by path within a tie, leaving out zeroes — the "most imported" and "imports the most" lists, shared by the text report and the JSON so the two cannot disagree.

Implementation

List<MapEntry<String, int>> ranked(
  Map<String, int> counts, {
  required Set<String> scope,
  int top = 10,
}) =>
    (counts.entries.where((e) => e.value > 0 && scope.contains(e.key)).toList()
          ..sort((a, b) {
            final byCount = b.value.compareTo(a.value);
            return byCount != 0 ? byCount : a.key.compareTo(b.key);
          }))
        .take(top)
        .toList();