sorted method

List<T> sorted()

Returns a new list containing all elements sorted in ascending order.

This extension method works on an iterable that contains elements which are Comparable. The method creates a new list from the iterable, sorts it in ascending order, and then returns it.

The sorting is done in-place on the newly created list.

Returns: List

Example:

List<int> numbers = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5];
var sortedNumbers = numbers.sorted(); // Returns [1, 1, 2, 3, 3, 4, 5, 5, 5, 6, 9]

Implementation

List<T> sorted() {
  var list = toList();
  list.sort();
  return list;
}