List.Sort
A list in order: ascending by default, descending, by a key or by a comparer.
Group: List. Power Query M has it too: a query that uses it is written as in Power BI.
Syntax
List.Sort(list as list, optional comparisonCriteria as any) as list
| Parameter | Type | Description |
|---|---|---|
list | list | The items to sort. |
comparisonCriteria | any optional | Order.Ascending or Order.Descending; a function of one item that gives the key to sort by; a list of such a function and an order; or a comparer of two items, such as Comparer.OrdinalIgnoreCase. |
Returns
list. The same items, sorted.
Remarks
- Null sorts before every other value.
- Text sorts by character code unless a comparer says otherwise: "Zebra" comes before "apple".
Examples
Largest first
List.Sort({120, 95, 300, 80}, Order.Descending)
{300, 120, 95, 80}
Without regard to capitals
List.Sort({"espresso", "Latte", "Cappuccino", "americano"}, Comparer.OrdinalIgnoreCase)
{"americano", "Cappuccino", "espresso", "Latte"}
Without the comparer the two capitalised names would come first.
Products by price, dearest first
List.Sort({[Product = "Tea", Price = 8], [Product = "Coffee", Price = 12.5], [Product = "Milk", Price = 2.9]}, {each [Price], Order.Descending})
{[Product = "Coffee", Price = 12.5], [Product = "Tea", Price = 8], [Product = "Milk", Price = 2.9]}
Related functions
List.Max, List.Reverse, Table.Sort, Comparer.OrdinalIgnoreCase.