Skip to content
Justice Tech School

Sorting and Iterables

sort changes a list in place with an optional comparison function. Iterables offer take, skip, first, last, any, every and toSet.

Example
void main() {
  var n = [5, 2, 9, 1];
  n.sort();
  print(n);
  n.sort((a, b) => b.compareTo(a));
  print(n.take(2).toList());
  print(n.any((x) => x > 8));
}

Exercise

Keep the first 2.

n.(2)

Quiz Test

Does sort return a new list?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.