0

Im trying to sort the following array by "likes" count. For example "likes" should be sorted 86, 30, 12 along with all its associated content. Thanks!

[  

   [

     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",
                      ],
              ],
      ],
]

I apologize in advance if the brackets are hard to read.

1
  • Doublequotes closing URLs in profile_picture attributes are missing... Commented Jul 9, 2015 at 9:10

2 Answers 2

1

I have refined your array such that it makes sense to use in playground.

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",
                ],
            ],
    ],
]

You can either use Objective C syntax and sort using NSSortDescriptor which in this particular case seems to be easier.

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

Or, you could also use plain Swift approach to sort array using sort block. The downside of this is you would need to cast the value to certain type until you reach the nested dictionary "likes"

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
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for refining my array, i'll make sure i do that next time before I post. Now pertaining to the code, the first approached worked using objective-c syntax but the second approach had an error stating "Consecutive statements on a line must be separated by';'". Like i said the first one worked for me and I'm rolling with it :-) but might want to edit the second one incase someone else needs it. Anyway thanks much, greatly appreciated!
Where exacttly do you have that error ? Are you not using Swift 2 ?
No I'm not, I'm using Xcode version 6.4, i believe that one has Swift 1.2.
0

If you call array.sort this excepts a closure which expects you to return true or false wether the left item is "higher" than the right one. all you have to do is compare the likes. You should take a look at this SO Swift how to sort array of custom objects by property value

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.