Navigation
Synopsis Sort the elements of a set.

Sort the elements of a set:
  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.

    This function lessThan (<) function should implement a strict partial order, meaning:
  3. that it is not reflexive, i.e. never a < a
  4. is anti-symmetric, i.e. never a < b && b < a.
  5. is transitive, i.e. if a < b and b < c then a < c.
Function
  1. list[&T] sort(set[&T] s)
  2. list[&T] sort(set[&T] l, bool (&T a, &T b) less)
Usage import Set;
Examples
rascal>import Set;
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"};
set[str]: {"mango","banana","pear","pineapple","grape","strawberry","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({"Anise", "Cayenne", "Saffron", "Blackcurrant"}) is

Question [3].
sort({"Guy Smiley"}) == 



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.