Skip to main content

order

For any type T,

order(collection: Bag<T>) : List<T>

Sorts a collection (in ascending order).

  • The collection is ordered by the natural order on elements.
    • That is, the same order used for comparing items with relational expressions such as less-than (<) and greater-than (>).
    • See comparisons for more detail on the order of values.
  • Note: x <= y if and only if order([x, y])[0] == x and order([x, y])[1] == y.
  • Note that this method accepts both Lists and Bags ─since Lists subtype Bags

Example

Sort a list of numbers in ascending order:

numbers = [3, 1, 4, 1, 5];
order(numbers) // Returns [1, 1, 3, 4, 5]

This is equivalent to the more verbose, albeit more flexible, comprehension:

numbers = [3, 1, 4, 1, 5];

foreach n in numbers
select n
order by n ascending

Notes

To sort in descending order, use the order by comprehension clause. See collections for more detail.

See Also

Guides