Sn1*_*kkN 1 sorting dictionary ios swift
我正在尝试按键(The Double)订购[Double:Article]字典.
我已经尝试了一切,现在我有以下代码:
// getting an array of sorted keys
let articlesKeys = articles.keys.sort{$0 > $1}
var sortedArticles = [Double:Article]()
// trying to fill the new dictionary in a descending keys order
for key in articlesKeys
{
sortedArticles[key] = articles[key]
}
// replacing the old dictionary (articles)
// with the new and ordered one (sortedArticles)
articles.removeAll()
articles = sortedArticles
Run Code Online (Sandbox Code Playgroud)
问题是订购了"articleKeys"
print(articleKeys)===> [220,218,110]
但是当我打印出"sortedArticles"或新的"文章"时:
print(sortedArticles)===> [110:X],[220:Y],[218:Z]
字典尚未订购:(
字典是无序的 - 这是它们的本质.你无法对它们进行排序.但是你可以以某种方式使用数组来存储键和值作为其值.
点击此处了解更多相关信息,请访问http://rypress.com/tutorials/objective-c/data-types/nsdictionary
实际上有一些东西会帮助你.了解有关keysSortedByValueUsingComparator的更多信息: