Skip to main content

List.Sort

Sorts a list of data according to the criteria specified.

Syntax

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

Remarks

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. This function will be passed two items from the list (any two items, in any order). The function should return one of the following values:
    • -1: The first item is less than the second item.
    • 0: The items are equal.
    • 1: The first item is greater than the second item.
    Value.Compare is a method that can be used to delegate this logic.

Examples

Example #1

Sort the list {2, 3, 1}.

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

Result:

{1, 2, 3}

Example #2

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

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

Result:

{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))

Result:

{3, 2, 1}

Category

List.Ordering