List.Sort
This function sorts a list of data according to specified optional criteria. The optional parameter, comparisonCriteria, can be used to define the criteria for comparison. It can take the following va...
This function sorts a list of data according to specified optional criteria. The optional parameter, comparisonCriteria, can be used to define the criteria for comparison. It can take the following values:
- To specify the order, the comparison criteria can be one of the Order enum values (Order.Descending, Order.Ascending).
- To calculate a key for sorting, a function with 1 argument can be used.
- To choose a key and control the order, the comparison criteria can be a list including the key and order ({each 1 / _, Order.Descending}).
- For full control over the comparison process, a function with 2 arguments can be used. This function will receive two items from the list (in any order) and 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.
The Value.Compare method can be utilized to delegate this comparison logic.
Arrange the set {2, 3, 1} in ascending order.
Syntax: Power Query MList.Sort({2, 3, 1})
Result: {1, 2, 3}
Arrange the set {2, 3, 1} in descending order.
Syntax: Power Query MList.Sort({2, 3, 1}, Order.Descending)
Result: {3, 2, 1}
Arrange the set {2, 3, 1} in descending order using the Value.Compare method.
Syntax: Power Query MList.Sort({2, 3, 1}, (x, y) => Value.Compare(1/x, 1/y))
Result: {3, 2, 1}