如何按值的升序对字典进行排序?
我有这样的东西:
<script type="text/javascript">
$('#searchbar').focus();
</script>
我想按其值的升序排列字典,如下所示:
["user1": 3764, "user5": 23, "user87": 875, "user21": 54987, ... ]
答案 0 :(得分:1)
尝试一下。
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 )]