### 5.38.18 Sort : sort

sort takes as argument a list or an expression.

- For a list,
  sort returns the list sorted in increasing order.
  Input :
  sort([3,4,2])
  Output :
  [2,3,4]
- For an expression,
  sort sorts and collects terms in sums and products.
  Input :
  sort(exp(2*ln(x))+x*y-x+y*x+2*x)
  Output :
  2*x*y+exp(2*ln(x))+x
  Input :
  simplify(exp(2*ln(x))+x*y-x+y*x+2*x)
  Output :
  x`^`2+2*x*y+x

sort accepts an optional second argument, which is a bivariate function returning 0 or 1. If provided, this function will be used to sort the list, for example (x,y)->x>=y may be used as second argument to sort the list in decreasing order. This may also be used to sort list of lists (that sort with one argument does not know how to sort).
Input :

sort([3,4,2],(x,y)->x>=y)

Output :

[4,3,2]