Swift (Xcode 7.3.1) and sorting using values in a Dictionary
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",


More about “Swift (Xcode 7.3.1) and sorting using values in a Dictionary” related questions

xcode upgrade to 7.3.1 from 7.2.1

Right now i'm using xcode 7.2.1 and planning to upgrade to 7.3.1, my question is, xcode 7.3.1 has swift 2.2 and 7.2.1 has swift 2.1. there are some new functionalities added and old ones are remove...

Show Detail

Compiling Swift source files stuck xcode 7.3.1

I am trying to archive my recent app . I am using xcode 7.3.1 and the code is mostly in swift. It runs on simulator , no issues in that. When I try to archive it , it stucks in compiling swift source

Show Detail

Will Xcode 7.3.1 work on macOS 10.12?

I want to install and try out the new features of macOS 10.12. Also I really want to work on Xcode 8.0. But the same time I have some client projects which should written on Swift 2.2 [ Xcode 7.3.1...

Show Detail

Swift Code in XCode 7.3.1 not working in iOS 10

I am building an iOS app using Swift 2.2, XCode 7.3.1, which is going to go live very soon. I upgraded my iPhone to iOS 10.0 and now when I try to run my code in my iPhone, I am getting the error -...

Show Detail

Xcode 7.3.1 crashing with swift code when debugging with breakpoints

I have updated the Xcode to 7.3.1 but still Xcode is crash when I am debugging the swift code. I also follow this Solution https://stackoverflow.com/a/36172742/1915700 But that particular option i...

Show Detail

Xcode 7.3.1 app submission to the Appstore

Does Apple still accept apps built using Xcode 7.3.1 (Swift 2.2) for the AppStore ? Or does it have to be migrated to Xcode 8 (at least Swift 2.3 or above) ?

Show Detail

How to integrate SwiftyJSON Cocoapod into Xcode 7.3.1 Swift 2.2

I'm using cocoapods to install SwiftyJSON into Xcode 7.3.1 Swift 2.2 and I get 158 build errors. This is the pod I'm using: pod 'SwiftyJSON', :git =&gt; 'https://github.com/SwiftyJSON/SwiftyJSON.g...

Show Detail

running app in ios 10 using xcode 7.3.1

Hi i have developed a app using Xcode 7.3.1 and swift 2.2 my app will execute upto the deployed target 9.3 but i want to execute my app in latest versions also that is iOS10. I don't want to update...

Show Detail

Uploading app to itunes from xcode 7.3.1 for iOS 10

My current project app settings are xcode 7.3.1,I am using swift 2.2. My app supports iOS 8 and above. With the recent changes to security and privacy setting for iOS 10, I wanted to know if upload...

Show Detail

Upgrade Xcode 7.3.1 Project to Xcode 8.0

I want to upgrade my Xcode 7.3.1 Project to Xcode 8. My project was written in swift 2.2 in Xcode 7 but now I want to upgrade to Xcode 8 with swift 3.0. My project contains some pod-files library w...

Show Detail