如何使用多个词典和数组对数组进行排序

时间:2015-07-09 09:03:31

标签: arrays json swift sorting

我试图通过“喜欢”计数对以下数组进行排序。例如,“likes”应该与其所有相关内容一起分类86,30,12。谢谢!

[  

   [

     location: "Zoo Miami",   
     attribution: https://instagram.com,   
     tags: [    
             zoomiami,  
             iphone6plus,  
             catfish  
            ],   
     likes: [  
              count: 86;  
              data:  [  
                       full_name: "Followers 2015",
                       id: 1570,
                       profile_picture: "https://igcdn-photos.com,
                       username: "followers_2015_new",
                      ],
              ],
      ],

   [

     location: "California",   
     attribution: https://instagram.com,   
     tags: [    
             California,  
             iphone,  
             cat  
            ],   
     likes: [  
              count: 12;  
              data:  [  
                       full_name: "Jake Smith",
                       id: 1450,
                       profile_picture: "https://igcdn-photos.com,
                       username: "Jake_Smith",
                      ],
              ],
      ],

   [

     location: "Philadelphia",   
     attribution: https://instagram.com,   
     tags: [    
             philly,  
             skateboard,  
             vans  
           ],   
     likes: [  
              count: 30;  
              data:  [  
                       full_name: "John Jones",
                       id: 1210,
                       profile_picture: "https://igcdn-photos.com,
                       username: "John Jones",
                      ],
              ],
      ],
]

如果括号难以阅读,我会提前道歉。

2 个答案:

答案 0 :(得分:1)

我已经对你的数组进行了改进,以便在游乐场中使用它。

var a = [
            [
                "location": "Zoo Miami",
                "attribution": "https://instagram.com",
                "tags": [
                    "zoomiami",
                    "iphone6plus",
                    "catfish"
                ],

                "likes": [
                    "count": 86,
                    "data":  [
                        "full_name": "Followers 2015",
                        "id": 1570,
                        "profile_picture": "https://igcdn-photos.com",
                        "username": "followers_2015_new",
                    ],
                ],
            ],

            [
                "location": "Philadelphia",
                "attribution": "https://instagram.com",
                "tags": [
                    "Philly",
                    "iphone",
                    "newData"
                ],

                "likes": [
                    "count": 12,
                    "data":  [
                        "full_name": "Jake 2015",
                        "id": 1850,
                        "profile_picture": "https://igcdn-photos.com",
                        "username": "Jake 2015_new",
                    ],
                ],
            ],

        [
            "location": "California",
            "attribution": "https://instagram.com",
            "tags": [
                "California",
                "skateboard",
                "vans"
            ],

            "likes": [
                "count": 30,
                "data":  [

                    "full_name": "John Jones",
                    "id": 1450,
                    "profile_picture": "https://igcdn-photos.com",
                    "username": "John Jones",
                ],
            ],
    ],
]

您可以使用Objective C语法并使用NSSortDescriptor进行排序,在这种特殊情况下,这似乎更容易。

let sortDescriptor = NSSortDescriptor(key: "likes.count", ascending: true)
var newArray = (a  as NSArray).sortedArrayUsingDescriptors([sortDescriptor])

或者,您也可以使用普通的Swift方法使用排序块对数组进行排序。这样做的缺点是你需要将值转换为某种类型,直到你到达嵌套词典“喜欢”

let sortedArray = a.sort { a, b in
    guard let aLikes = a["likes"] as? [String: AnyObject], bLikes = b["likes"] as? [String: AnyObject] else {
        return false
    }
    guard let aLikesCount = aLikes["count"] as? Int, bLikesCount = bLikes["count"] as? Int  else {
        return false
    }
   return aLikesCount < bLikesCount
}

答案 1 :(得分:0)

如果你调用array.sort,那么除了一个闭包,它要求你返回true或false,左边的项是&#34;更高&#34;比正确的。你所要做的就是比较喜欢的东西。你应该看看这个SO Swift how to sort array of custom objects by property value