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.
- That is, the same order used for comparing items with relational expressions such as less-than (
- Note:
x <= yif and only iforder([x, y])[0] == xandorder([x, y])[1] == y. - Note that this method accepts both
ListsandBags─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
- Collections - Comprehensive guide on Lists, Bags, and ordering
- Comprehensions - Using order by clause