Navigation
Synopsis Sort the elements of a list.
Function
  1. list[&T] sort(list[&T] lst)
  2. list[&T] sort(list[&T] l, bool (&T a, &T b) less)
Usage import List;
Description Sort the elements of a list:
  1. Use the built-in ordering on values to compare list elements.
  2. Give an additional lessThan function that will be used to compare elements.
Examples
rascal>import List;
ok
rascal>import String;
ok
rascal>sort([10, 4, -2, 11, 100, 5]);
list[int]: [-2,4,5,10,11,100]
rascal>fruits = ["mango", "strawberry", "pear", "pineapple", "banana", "grape", "kiwi"];
list[str]: ["mango","strawberry","pear","pineapple","banana","grape","kiwi"]
rascal>sort(fruits);
list[str]: ["banana","grape","kiwi","mango","pear","pineapple","strawberry"]
rascal>sort(fruits, bool(str a, str b){ return size(a) > size(b); });
list[str]: ["strawberry","pineapple","banana","mango","grape","kiwi","pear"]

Questions
Question [1]. Sorting a listing with N elements gives a list with:




Question [2].
The type of sort(["Two-Headed Monster", "Bruno"]) is

Question [3].
sort(["R2-D2", "A", "Mango", "Cayenne"]) == 



Is this page unclear, or have you spotted an error? Please add a comment below and help us to improve it. For all other questions and remarks, visit ask.rascal-mpl.org.