重新排序字典的数组雨燕 [英] Reorder array of Dictionary By swift

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

问题描述

我工作的iOS应用程序通过SWIFT!

I'm working on ios app by swift!

这是我需要由价格值以显示从字典顺序的数组中的数据。
我怎样才能重新排序数组

An i need to display the data from the array of dictionary order by the price value. How can i reorder this array

(
        {
        price = 269600;
        userid = "facebook:851661711521620";
    },
        {
        price = 294600;
        userid = "facebook:851661711521620";
    },
        {
        price = 1345600;
        userid = "facebook:851661711521620";
    },
        {
        price = 287600;
        userid = "facebook:851661711521620";
    },
        {
        price = 1344600;
        userid = "facebook:851661711521620";
    },
        {
        price = 1343600;
        userid = "facebook:851661711521620";
    },
        {
        price = 1333600;
        userid = "facebook:851661711521620";
    },
        {
        price = 1332600;
        userid = "facebook:851661711521620";
    },
        {
        price = 1322600;
        userid = "facebook:851661711521620";
    },
        {
        price = 322600;
        userid = "facebook:851661711521620";
    },
        {
        price = 302600;
        userid = "facebook:851661711521620";
    },
        {
        price = 300600;
        userid = "facebook:851661711521620";
    },
        {
        price = 291600;
        userid = "facebook:851661711521620";
    },
        {
        price = 282600;
        userid = "facebook:851661711521620";
    },
        {
        price = 299600;
        userid = "facebook:851661711521620";
    }
)

按价格从最高到最低订货?
我需要显示的数据是这样

to be ordered by price from The highest to The lowest? i need to display the data to be like this

任何想法我如何能做到这一点谢谢!

Any ideas how can i do it Thanks!

谢谢!

编辑添加一些实际的code。

Edited add some actual code.

self.currentBidDict = snapshot.value["current_bid_id"] as! NSDictionary

for(var i = 0;i <= self.currentBidDict.count - 1;i++){
    self.currentBidDictReOrder.insertObject(self.currentBidDict.allValues[i], atIndex: 0)
    println(self.currentBidDictReOrder)
}

然后后,我printIn我得到了上面的问题阵列。

Then after i printIn i got array above in the question.

推荐答案

有一个函数,排序,即采取任何序列,并返回一个排序的数组。因为字典是(键,值)对序列,就可以把字典中的排序。

There is a function, sort, that takes any sequence, and returns a sorted array. Since dictionaries are sequences of (key,value) pairs, you can pass the dictionary in to sort.

如果你传递不自然排序(即序列的元素是可比,它的字典都没有),你也需要提供一个封闭告诉排序如何排序的元素。在你的情况,你希望那封闭在字典特定键比较值的。

If what you pass in isn’t naturally sortable (i.e. the elements of the sequence are Comparable, which dictionaries aren’t), you need to also supply a closure telling sort how to order the elements. In your case, you want that closure to compare the value for a specific key in the dictionaries.

最后,你想通过一个值排序,但查找在字典中的一个关键返回一个可选的(因为密钥可能不存在)。但是,这是好的!因为自选有一个&LT; 运营商这样的作品,只要有什么可选包含媲美。除非,它看起来像你有一本字典任何 AnyObject (因为你有两个字符串和整数有),所以你必须给他们隐蔽的东西,你可以比较一下。

Finally, you want to sort by a value, but looking up a key in a dictionary returns an optional (because the key might not be there). But, this is fine! Because optionals have a < operator that works, so long as what the optional contains is comparable. Except, it looks like you’ve got a dictionary of Any or AnyObject (because you have both strings and integers in there), so you’ll have to covert them to something you can compare.

把它放在一起,

sorted(dict) { ($0["price"] as? Int) > ($1["price"] as? Int) }

您可能要调整了有点取决于你的字典实际上包含。另外,我强烈建议寻找创造一个更强类型的结构和填充与您的数据,而不是把它留在字典的形式是这样的。

You might want to tweak that as? a bit depending on what your dictionary actually contains. Also, I’d strongly suggest looking at creating a more strongly-typed struct and populating that with your data, rather than leaving it in dictionary form like this.

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

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