如何重新排序NSMutableDictionaries的NSMutableDictionary [重复](how to reorder an NSMutableDictionary of NSMutableDictionaries [duplicate])
这个问题在这里已有答案:
在使用json数据的循环中,我正在创建一个NSMutableDictionary,在其中我存储了json解析的值:
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 }
在循环结束时,我将jsonValues mutableDictionary放在一个全局var中,另一个名为nsDict的NSMutableDictionary。
我想重新排序我的字典词典,使用
["distance"]
值,这是在metters中,所以我可以在从较低距离到较高距离的tableView中显示它。 我已经堆栈溢出和谷歌搜索了一段时间,但我发现只有examples.sortdictionary.sort()
例子,这对我来说是无用的,因为我使用NSMutableDictionary。 找不到NsMutableDictionary的任何示例,并且不太了解apple文档。 在另一种语言中,比如php,我会用两个do{}while
来做这个但是我无法用swift 2用给定的方法做到这一点。 有人可以帮帮我吗?这是NSMutableDictionaries的NSMutableDictionary这就是为什么我认为解析技术与订购一个NSMutableDictionary不同
This question already has an answer here:
- Sort an NSMutableDictionary 6 answers
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 withdictionary.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 twodo{}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
原文: