字典排序后返回排序字典 [英] Return Sorted Dictionary after sorting of dictionary

查看:161
本文介绍了字典排序后返回排序字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在字典中,我们有多个键值对.我们对它们进行了排序,返回类型为[(key:value)]对.但是我们需要按排序顺序返回相同的时间.请为此提供解决方案.

In dictionary we have multiple key value pairs. We sorted these, and return type is [(key:value)] pair. But we need the same return time in sorted order. Please give solution for that.

推荐答案

字典在Swift中没有顺序,因此排序字典"这一概念毫无意义. LiteralDictionary以慢速键查找为代价维护排序顺序,但是只能从文字中创建它,而不能通过对现有字典进行排序来创建.您可以创建自己的有序字典实现,但这将需要大量工作,而且几乎可以肯定,这是以牺牲键查找速度为代价的.您可能会查看 Swift中的有序词典和类似内容,但是最好问问自己为什么想要这个您有一个键-值对的排序数组,因此只需使用它并停止即可.

A Dictionary has no order in Swift, so the notion "sorted dictionary" is meaningless. LiteralDictionary maintains sort order at the expense of slow key lookup, but it can be created only from a literal — not by sorting an existing dictionary. You could create your own ordered dictionary implementation, but it would be a lot of work, and again would almost certainly be at the expense of key lookup speed. You might look at Ordered dictionary in Swift and similar, but would do better to ask yourself why you want this at all; you have a sorted array of key–value pairs, so just use that and stop.

这篇关于字典排序后返回排序字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆