Table.Sort
語法
Table.Sort(table as table, comparisonCriteria as any) as table
關於
以 { { col1, comparisonCriteria }, {col2} } 的格式,使用一或多個資料行名稱與選擇性 comparisonCriteria
的清單來排序 table
。
範例 1
依據 "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"}
)
輸出
Table.FromRecords({
[OrderID = 1, CustomerID = 1, Item = "Fishing rod", Price = 100],
[OrderID = 2, CustomerID = 1, Item = "1 lb. worms", Price = 5],
[OrderID = 3, CustomerID = 2, Item = "Fishing net", Price = 25],
[OrderID = 4, CustomerID = 3, Item = "Fish tazer", Price = 200],
[OrderID = 5, CustomerID = 3, Item = "Bandaids", Price = 2],
[OrderID = 6, CustomerID = 1, Item = "Tackle box", Price = 20],
[OrderID = 7, CustomerID = 5, Item = "Bait", Price = 3.25],
[OrderID = 8, CustomerID = 5, Item = "Fishing Rod", Price = 100],
[OrderID = 9, CustomerID = 6, Item = "Bait", Price = 3.25]
})
範例 2
依據 "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", Order.Descending}
)
輸出
Table.FromRecords({
[OrderID = 9, CustomerID = 6, Item = "Bait", Price = 3.25],
[OrderID = 8, CustomerID = 5, Item = "Fishing Rod", Price = 100],
[OrderID = 7, CustomerID = 5, Item = "Bait", Price = 3.25],
[OrderID = 6, CustomerID = 1, Item = "Tackle box", Price = 20],
[OrderID = 5, CustomerID = 3, Item = "Bandaids", Price = 2],
[OrderID = 4, CustomerID = 3, Item = "Fish tazer", Price = 200],
[OrderID = 3, CustomerID = 2, Item = "Fishing net", Price = 25],
[OrderID = 2, CustomerID = 1, Item = "1 lb. worms", Price = 5],
[OrderID = 1, CustomerID = 1, Item = "Fishing rod", Price = 100]
})
範例 3
依序根根據 "CustomerID" 和 "OrderID" 資料行來排序資料表,其中 "CustomerID" 以遞增順序排列。
使用方式
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"
}
)
輸出
Table.FromRecords({
[OrderID = 1, CustomerID = 1, Item = "Fishing rod", Price = 100],
[OrderID = 2, CustomerID = 1, Item = "1 lb. worms", Price = 5],
[OrderID = 6, CustomerID = 1, Item = "Tackle box", Price = 20],
[OrderID = 3, CustomerID = 2, Item = "Fishing net", Price = 25],
[OrderID = 4, CustomerID = 3, Item = "Fish tazer", Price = 200],
[OrderID = 5, CustomerID = 3, Item = "Bandaids", Price = 2],
[OrderID = 7, CustomerID = 5, Item = "Bait", Price = 3.25],
[OrderID = 8, CustomerID = 5, Item = "Fishing Rod", Price = 100],
[OrderID = 9, CustomerID = 6, Item = "Bait", Price = 3.25]
})