假设我有一个字典数组:
[ { "id": 2 }, { "id": 59 }, { "id": 31 } ... ]
Run Code Online (Sandbox Code Playgroud)
我怎样才能对它进行排序,使其按降序排列,按"id"排序?
我最初的方法是这样的:
Loop through each element, find the biggest one, and put it into a new array. Then, remove that from the element. Repeat.
Run Code Online (Sandbox Code Playgroud)
但我知道这是错误的,效率不高.
您可以在Swift中使用sort函数.像这样的东西:
let arr = [["id": 2], ["id": 59], ["id": 31]]
let sortedArr = arr.sort { Int($0["id"]!) > Int($1["id"]!) }
Run Code Online (Sandbox Code Playgroud)