This question already has answers here:
Sort Dictionary by Key Value
(3个答案)
去年关闭。
如何按值的升序对字典排序?
我有这样的事情:
我想按其值的升序排列字典,如下所示:
已排序的userRankDict:
[(键:“user5”,值:23),(键:“user87”,值:875),(键:“user1”,值:3764),(键:“user21”,值:54987)]
(3个答案)
去年关闭。
如何按值的升序对字典排序?
我有这样的事情:
["user1": 3764, "user5": 23, "user87": 875, "user21": 54987, ... ]
我想按其值的升序排列字典,如下所示:
["user5": 23, "user87": 875, "user1": 3764, "user21": 54987, ...]
最佳答案
尝试这个。
let userRankDict = ["user1": 3764, "user5": 23, "user87": 875, "user21": 54987]
let sortedUserRankDict = userRankDict.sorted{ $0.value < $1.value }
print(sortedUserRankDict)
已排序的userRankDict:
[(键:“user5”,值:23),(键:“user87”,值:875),(键:“user1”,值:3764),(键:“user21”,值:54987)]
关于swift - 如何按值的升序对字典排序? ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57578415/
10-11 18:08