List.Sort

Syntax

List.Sort(list as list, optional comparisonCriteria as any) as list  

About

Sorts a list of data, list, according to the optional criteria specified. An optional parameter, comparisonCriteria, can be specified as the comparison criterion. This can take the following values:

  • To control the order, the comparison criterion can be an Order enum value. (Order.Descending, Order.Ascending).
  • To compute a key to be used for sorting, a function of 1 argument can be used.
  • To both select a key and control order, comparison criterion can be a list containing the key and order ({each 1 / _, Order.Descending}).
  • To completely control the comparison, a function of 2 arguments can be used that returns -1, 0, or 1 given the relationship between the left and right inputs. Value.Compare is a method that can be used to delegate this logic.

Example 1

Sort the list {2, 3, 1}.

List.Sort({2, 3, 1})
1
2
3

Example 2

Sort the list {2, 3, 1} in descending order.

List.Sort({2, 3, 1}, Order.Descending)
3
2
1

Example 3

Sort the list {2, 3, 1} in descending order using the Value.Compare method.

List.Sort({2, 3, 1}, (x, y) => Value.Compare(1/x, 1/y))
3
2
1