user3033437 user3033437 - 5 months ago 43
Swift Question

How to sort NSmutableArray from one of it indexes (lat lng coordinates) in Swift

I'm new in swift and I'd know how to do that in php, but I'm lost with all those dictionaries and I have no idea how to do that in swift 2. I've been googling for a while and didn't found what I need.

I'm parsing a jSon and storing it's values in an NSMutableDictionary in a loop and at the end of the loop I store the NSMutableDictionary in an NSMutableArray, so at the end I have an NSMutableArray with 43 elements, and each element have about 10 keys with their values. I need to sort those 43 elements from their "distance" key and sort them descending. I don't know if that is posible with this current approach. The value of the key "distance" is an int number (meters). I don't know if to use an NSMutableDictionary inside an NSMutable Array is the correct approach to do this but I'm using it because it is possible to have string keys, and not numbers indexes, so for me it's easier to access the key "distance" than the index 8...

First I load the jSon content:

private func parseJson(json : NSMutableArray, tableView : UITableView){

var c : Int = 0
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

//more code like this....

TableData.append(Name)
nsDict.insertObject(jsonValues, atIndex: c)

c += 1
}


this is my NSMutableArray content after being loaded:
enter image description here

enter image description here

And this is the code I have this far. Im trying to load the sorted content in a new array, but in this new array some keys are missing.

//Reorder Array by shop distance from user...
var sortDescriptor:NSSortDescriptor = NSSortDescriptor(key: "distance", ascending: true)
var sortedArray : NSArray = nsDict.sortedArrayUsingDescriptors([sortDescriptor])//Crashes

print(sortedArray)


enter image description here

enter image description here

Answer

I've managed to sort the array with this technique:

created a new class with for my array

import Foundation
class JsonArrayValues  {

    init(){

    }
    var name = String()
    var distance = Float()
    var lat = Float()
    var lng = Float()
    var openingTime = String()
    var country = String()
    var code = String()
    var address = String()
    var categories = String()
    var city = String()
    var type = String()
    var brands = String()





}

I instantiated one before the loop:

var jsonArrData : [JsonArrayValues] = []

And another one inside the loop, in which I've added the values:

        var c : Int = 0
        for j in json {
            var jsonValues : JsonArrayValues = JsonArrayValues()

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

            c += 1
        }

And finally I've been able to call the function to reorder the array:

   //Reorder Array by shop distance from user...
    jsonArrData.sortInPlace({$0.distance < $1.distance})