-1

In a loop with json data I'm creating an NSMutableDictionary and inside it I'm storing json parsed values :

for j in json {
            var jsonValues = NSMutableDictionary()

//Create main value
            guard let value = j.valueForKey("value")?.valueForKey("value")! else{
                continue
            }

            //Get name
            guard let Name : String = (value.valueForKey("Name")?.valueForKey("en") as? String) else {
                continue
            }

            jsonValues["name"] = Name

            //Get geoposition
            guard let geoposition = value.valueForKey("geoposition") else {
                return
            }

            //Get lat
            if let lat = geoposition.valueForKey("lat") as? String {

                jsonValues["lat"] = lat
                let flat : Float = (lat as AnyObject).floatValue


                //Get lng
                if let lng = geoposition.valueForKey("lng") as? String{

                    jsonValues["lng"] = lng
                    let flng : Float = (lng as AnyObject).floatValue

                    let metters : CLLocationDistance = getDistance(flat, flng: flng)

                    jsonValues["distance"] = floor(metters)
                }
            }else{}


nsDict.setValuesForKeysWithDictionary(["\(c)" : jsonValues])
c += 1
}

at the end of the loop I put the jsonValues mutableDictionary inside a global var, another NSMutableDictionary, named nsDict.

I would like to reorder my dictionary of dictionaries, using the ["distance"] value, which is in metters, so I can show it in a tableView from lower distance to upper. I've been stackoverflowing and googling for a while, but I find only examples with dictionary.sort() which is useless for me since I use an NSMutableDictionary. Couldn't find any example with NsMutableDictionary and doesn't really understand the apple docs. In another language, like php, I would had do this with two do{}while But I can't manage to do it in swift 2 with the given methods . Anybody can help me?

This is an NSMutableDictionary of NSMutableDictionaries this is why I think the parsing technique is not same as to order one NSMutableDictionary

Alfro
  • 1,524
  • 2
  • 21
  • 37
  • May I ask why you're using `NSMutableDictionary` in the first place? – Alexander Jun 08 '16 at 14:12
  • @AMomchilov if you know a better approach of what I'm doing you are welcome to post it – Alfro Jun 08 '16 at 14:16
  • 1
    I don't know what you're doing or trying to achieve. 1) From what I see here, you're not using any legacy frameworks that would require you to use `Foundation`'s collections (`NSArray`,`NSDictionary`, etc.). 2) This seems like a better fit for structs, which could conform to the `Comparable` interface. That would make comparison a piece of cake. – Alexander Jun 08 '16 at 14:34
  • 1
    My suggestion: restart from scratch, using Swift types (Dictionary, Array, struct) and Swift methods to sort them, instead of Foundation's ones. You will *feel* better, which is always good, and your code will have less ambiguities. – Eric Aya Jun 08 '16 at 14:36
  • ok I think it's the better approach too so let's start from scratch :( – Alfro Jun 08 '16 at 14:38
  • 1
    This seems like a case for structs. If you have many dictionaries with the same keys, it's a fair indication you should be using structs. – Alexander Jun 08 '16 at 14:47

1 Answers1

3

NSMutatbleDictionary doesn't retain order (sorted or insertion). You can't sort it.

You need a different approach, such as using an NSArray of sorted keys. See NSDictionary - Sorting Dictionaries.

Alexander
  • 59,041
  • 12
  • 98
  • 151