Table.Sort

Syntax

Table.Sort(table as table, comparisonCriteria as any) as table

About

Sorts the table using the list of one or more column names and optional comparisonCriteria in the form { { col1, comparisonCriteria }, {col2} }.

Example 1

Sort the table on column "OrderID".

Table.Sort(Table.FromRecords({[OrderID = 1, CustomerID = 1, Item = "Fishing rod", Price = 100.0], [OrderID = 2, CustomerID = 1, Item = "1 lb. worms", Price = 5.0], [OrderID = 3, CustomerID = 2, Item = "Fishing net", Price = 25.0], [OrderID = 4, CustomerID = 3, Item = "Fish tazer", Price = 200.0], [OrderID = 5, CustomerID = 3, Item = "Bandaids", Price = 2.0], [OrderID = 6, CustomerID = 1, Item = "Tackle box", Price = 20.0], [OrderID = 7, CustomerID = 5, Item = "Bait", Price = 3.25], [OrderID = 8, CustomerID = 5, Item = "Fishing Rod", Price = 100.0], [OrderID = 9, CustomerID = 6, Item = "Bait", Price = 3.25]}), {"OrderID"})
OrderID CustomerID Item Price
1 1 Fishing rod 100
2 1 1 lb. worms 5
3 2 Fishing net 25
4 3 Fish tazer 200
5 3 Bandaids 2
6 1 Tackle box 20
7 5 Bait 3.25
8 5 Fishing Rod 100
9 6 Bait 3.25

Example 2

Sort the table on column "OrderID" in descending order.

Table.Sort(Table.FromRecords({[OrderID = 1, CustomerID = 1, Item = "Fishing rod", Price = 100.0], [OrderID = 2, CustomerID = 1, Item = "1 lb. worms", Price = 5.0], [OrderID = 3, CustomerID = 2, Item = "Fishing net", Price = 25.0], [OrderID = 4, CustomerID = 3, Item = "Fish tazer", Price = 200.0], [OrderID = 5, CustomerID = 3, Item = "Bandaids", Price = 2.0], [OrderID = 6, CustomerID = 1, Item = "Tackle box", Price = 20.0], [OrderID = 7, CustomerID = 5, Item = "Bait", Price = 3.25], [OrderID = 8, CustomerID = 5, Item = "Fishing Rod", Price = 100.0], [OrderID = 9, CustomerID = 6, Item = "Bait", Price = 3.25]}), {"OrderID", Order.Descending})
OrderID CustomerID Item Price
9 6 Bait 3.25
8 5 Fishing Rod 100
7 5 Bait 3.25
6 1 Tackle box 20
5 3 Bandaids 2
4 3 Fish tazer 200
3 2 Fishing net 25
2 1 1 lb. worms 5
1 1 Fishing rod 100

Example 3

Sort the table on column "CustomerID" then "OrderID", with "CustomerID" being in ascending order.

Table.Sort(Table.FromRecords({[OrderID = 1, CustomerID = 1, Item = "Fishing rod", Price = 100.0], [OrderID = 2, CustomerID = 1, Item = "1 lb. worms", Price = 5.0], [OrderID = 3, CustomerID = 2, Item = "Fishing net", Price = 25.0], [OrderID = 4, CustomerID = 3, Item = "Fish tazer", Price = 200.0], [OrderID = 5, CustomerID = 3, Item = "Bandaids", Price = 2.0], [OrderID = 6, CustomerID = 1, Item = "Tackle box", Price = 20.0], [OrderID = 7, CustomerID = 5, Item = "Bait", Price = 3.25], [OrderID = 8, CustomerID = 5, Item = "Fishing Rod", Price = 100.0], [OrderID = 9, CustomerID = 6, Item = "Bait", Price = 3.25]}), {{"CustomerID", Order.Ascending}, "OrderID"})
OrderID CustomerID Item Price
1 1 Fishing rod 100
2 1 1 lb. worms 5
6 1 Tackle box 20
3 2 Fishing net 25
4 3 Fish tazer 200
5 3 Bandaids 2
7 5 Bait 3.25
8 5 Fishing Rod 100
9 6 Bait 3.25