【问题标题】:Reorder array of Dictionary By swift通过 swift 重新排序字典数组
【发布时间】:2015-05-04 22:19:16
【问题描述】:

我正在通过 swift 开发 ios 应用程序!

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

(
        {
        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";
    }
)

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

任何想法我该怎么做谢谢!

谢谢!

编辑添加一些实际代码。

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 之后,我在问题中得到了上面的数组。

【问题讨论】:

    标签: ios arrays swift


    【解决方案1】:

    有一个函数sort,它接受任何序列,并返回一个排序数组。由于字典是(key,value) 对的序列,因此您可以将字典传入进行排序。

    如果您传入的内容不是自然可排序的(即序列的元素是 Comparable,而字典不是),您还需要提供一个闭包告诉 sort 如何对元素进行排序。在您的情况下,您希望该闭包比较字典中特定键的值。

    最后,您想按值排序,但在字典中查找键会返回一个可选值(因为该键可能不存在)。但是,这很好!因为可选项有一个可以工作的&lt; 运算符,只要可选项包含的内容是可比较的。除了,看起来你有一个 AnyAnyObject 的字典(因为你有字符串和整数),所以你必须将它们转换为你可以比较的东西。

    把它们放在一起,然后:

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

    您可能需要根据字典实际包含的内容稍微调整 as?。另外,我强烈建议您考虑创建一个更强类型的结构并用您的数据填充它,而不是像这样将其保留为字典形式。

    【讨论】:

    • 我得到了接受类型参数列表的“排序”的重载
    • 你需要发布实际代码,如果不知道所涉及的真实类型,就不可能告诉你确切的语法。
    【解决方案2】:

    您可以通过以下方式使用sort函数:

    var dict =  [[
            "price" : 1111,
            "userid" : "facebook:851661711521620"
            ],
            [
            "price" : 12222,
            "userid" : "facebook:851661711521620"
            ],
            [
            "price" : 144444,
            "userid" : "facebook:851661711521620"
            ]]       
    
    // sort by price 
    dict.sort {
        x, y in
        let item1 = x["price"] as! Int
        let item2 = y["price"] as! Int
        return item1 > item2
    }
    

    输出应该是这样的:

    [[price: 144444, userid: facebook:851661711521620], [price: 12222, userid: facebook:851661711521620], [price: 1111, userid: facebook:851661711521620]]
    

    希望对你有所帮助。

    【讨论】:

    • sorted(dictionary) { $0.0 &lt; $1.0 } 还不够吗?没有真正需要给它们命名,将它们转换为 Int 并比较它们吗?
    • @JacobsonTalom 如果你按照你说的去做,那么你会遇到类似Cannot find an overload for 'sorted' that accepts an argument list of type '([Dictionary&lt;String, NSObject&gt;], (_, _) -&gt; _)' 这样的编译错误,因为sortsorted 函数不知道里面的元素是怎样的。自己试试吧
    • 很晚了,我该睡觉了.. :p 我的错。
    • 无需强制解包,可选项可直接与&gt;比较
    • 我试过 self.currentBidDictReOrder.sort { x, y in let item1 = x["price"] as! int let item2 = y["price"] as! int return item1 > item2 } 但仍然得到 NSmutablearray 没有成员名排序?
    猜你喜欢
    • 2018-10-25
    • 1970-01-01
    • 1970-01-01
    • 2016-03-03
    • 1970-01-01
    • 1970-01-01
    • 2018-07-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多