Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sorting dictionary in Swift 3

Trying to sort an NSMutableDictionary in Swift 3, code from Swift 2 doesn't work for me anymore (various errors).

I am trying to use the following code to sort my dictionary by its values, which are floats:

var sortedDict = unsortedDict.allValues.sorted({ $0 < $1 }).flatMap({ floatedlotterydictionary[$0] })

Essentially, I want this unsorted dictionary...

{
a = "1.7";
b = "0.08";
c = "1.4";
}

...to turn into this sorted dictionary...

{
b = "0.08";
c = "1.4";
a = "1.7";
}

But using that line of code from above returns the error "argument type anyobject does not conform with type NSCopying" for the $0 < $1 part. So how can I sort a dictionary by its values in Swift 3?

(Note: That line of code came in part from this answer.)

I am using Swift 3 in Xcode 8 beta 1.

like image 568
owlswipe Avatar asked Jul 16 '16 14:07

owlswipe


People also ask

What is a dictionary key in Swift?

Swift 4 dictionaries use unique identifier known as a key to store a value which later can be referenced and looked up through the same key. Unlike items in an array, items in a dictionary do not have a specified order. You can use a dictionary when you need to look up values based on their identifiers. A dictionary key...

What is swift 4 Dictionary?

Swift 4 puts strict checking which does not allow you to enter a wrong type in a dictionary even by mistake. Swift 4 dictionaries use unique identifier known as a key to store a value which later can be referenced and looked up through the same key. Unlike items in an array, items in a dictionary do not have a specified order.

How to create dictionary from an array in Swift 4?

var someDict: [Int:String] = [1:"One", 2:"Two", 3:"Three"] Swift 4 allows you to create Dictionary from arrays (Key-Value Pairs.) You can use the following simple syntax to create an empty dictionary whose key will be of Int type and the associated values will be strings − Here is an example to create a dictionary from a set of given values −

How to iterate over the elements of a dictionary in Swift?

We use the for loop to iterate over the elements of a dictionary. For example, We can use the count property to find the number of elements present in a dictionary. For example, In Swift, we can also create an empty dictionary. For example, In the above example, we have created an empty dictionary. Notice the expression


2 Answers

Ok. The collection methods available on a normal dictionary are still available, however the type of objects is not enforced, which adds an inherit unsafely.

In the docs the sorted takes a closure which lets us access the key and value at the same time and consider it as one element and hence sort your dictionary.

let k: NSMutableDictionary = ["a" : 1.7, "b" : 0.08, "c" : 1.4]
print(k.sorted(isOrderedBefore: { (a, b) in (a.value as! Double) < (b.value as! Double) }))

The casting is required, as the type of a.value and b.value is AnyObject. This works on my computer, running Xcode Version 8.0 beta (8S128d).

like image 155
Mrwerdo Avatar answered Nov 15 '22 06:11

Mrwerdo


There's also this way:

let k = ["a" : 1.7, "b" : 0.08, "c" : 1.4]

print(k.flatMap({$0}).sort { $0.0.1 < $0.1.1 })
like image 40
FranMowinckel Avatar answered Nov 15 '22 07:11

FranMowinckel