NickName:NetCod Ask DateTime:2016-08-24T14:15:32 Swift (Xcode 7.3.1) and sorting using values in a Dictionary I have an array of dictionaries that appears as below and I want to sort and store it such that the objects are sorted based on the 'value' of the 'Like' key ? How do I do this in Swift ? Thanks. { Dislike = 0; Like = 5; userName = T; } { Dislike = 0; Like = 0; userName = S; } { Dislike = 0; Like = 10; userName = N; } Copyright Notice:Content Author:「NetCod」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.Link to original article:https://stackoverflow.com/questions/39115683/swift-xcode-7-3-1-and-sorting-using-values-in-a-dictionary Answers Edder Núñez 2016-08-24T06:22:37 I suppose that's an array of dictionaries?\n\nIf it is the case, you can sort them as follows\n\n//AnyObject in case your number is an NSNumber\nvar array: [[String:AnyObject?]] = yourArray \n\narray.sort{ $0[\"Like\"]!! > $1[\"Like\"]!! }\n\n\nbasically it mutates the array and sort based on the \"Like\" key.", Sweeper 2016-08-24T06:35:34 This data structure looks really weird you know. I think it is a [[String: Any]]. It should be declared like this:\n\nlet data: [[String: Any]] = [\n [\n \"Dislike\": 0,\n \"Like\": 5,\n \"username\": \"T\"],\n\n [\n \"Dislike\": 0,\n \"Like\": 0,\n \"username\": \"S\"],\n\n [\n \"Dislike\": 0,\n \"Like\": 10,\n \"username\": \"N\"],\n]\n\n\nTo sort it, simply use the sort function. In the closure, you should return whether the first parameter should be ordered before the second parameter. In this case, you would like to only compare the values corresponding to the key \"Like\". The closure body would look something like this: (pseudocode)\n\nfirstParameter[\"Like\"] < secondParameter[\"Like\"]\n\n\nas I said, that is pseudocode. The real code is a little more complicated:\n\ndata.sort { ($0[\"Like\"]! as! Int) < ($1[\"Like\"] as! Int) }\n",