如何从其中一个指标(lat lng坐标)中对NSmutableArray进行排序?

时间:2021-11-14 03:11:20

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.

我是swift的新成员,我知道如何在php中使用它,但是我丢失了所有这些字典,我不知道如何在swift 2中做到这一点。我搜索了一段时间,没有找到我需要的东西。

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...

我解析一个jSon并将它的值存储在一个循环中的NSMutableDictionary中,在循环的末尾,我将NSMutableDictionary存储在NSMutableArray中,所以在最后,我有一个带有43个元素的NSMutableArray,每个元素都有大约10个键值。我需要从它们的“距离”键中对这43个元素进行排序,并对它们进行排序。我不知道这是否与目前的方法相吻合。键“距离”的值是一个整数(米)。我不知道是否在NSMutable数组中使用NSMutableDictionary是正确的方法,但我使用它是因为它可能有字符串键,而不是数字索引,所以对我来说,访问键“距离”比索引8更容易……

First I load the jSon content:

首先我加载jSon内容:

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: 如何从其中一个指标(lat lng坐标)中对NSmutableArray进行排序?

这是我在加载后的NSMutableArray内容:

如何从其中一个指标(lat lng坐标)中对NSmutableArray进行排序?

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)

如何从其中一个指标(lat lng坐标)中对NSmutableArray进行排序?

如何从其中一个指标(lat lng坐标)中对NSmutableArray进行排序?

2 个解决方案

#1


0  

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}) 

#2


0  

One of your first steps in any non-trivial project really should be to spend some time looking around on github for tools that simplify your problem. In this case, you'd find there are so very many tools to simplify working with JSON in Swift. I'd suggest you look at EVReflection and Gloss particularly, although there are also many people who use SwiftyJSON.

在任何非平凡的项目中,您的第一步都应该是花些时间在github上寻找简化问题的工具。在这种情况下,您会发现在Swift中有很多工具可以简化JSON的工作。我建议您特别关注EVReflection和Gloss,尽管也有很多人使用swift json。

You don't mention how you're accessing the network; you could look at AFNetworking or Alamofire. The latter also has AlamofireJsonToObjects to help.

你没有提到你是如何访问网络的;你可以看看AFNetworking或Alamofire。后者也有AlamofireJsonToObjects提供帮助。

I also found JSONExport to be incredibly useful.

我还发现JSONExport非常有用。

You'd be spending a lot less time futzing with details as in this unfortunate question and more getting on with your larger goal.

在这个不幸的问题上,你会花更少的时间来处理细节问题,而更多的是为了实现更大的目标。

#1


0  

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}) 

#2


0  

One of your first steps in any non-trivial project really should be to spend some time looking around on github for tools that simplify your problem. In this case, you'd find there are so very many tools to simplify working with JSON in Swift. I'd suggest you look at EVReflection and Gloss particularly, although there are also many people who use SwiftyJSON.

在任何非平凡的项目中,您的第一步都应该是花些时间在github上寻找简化问题的工具。在这种情况下,您会发现在Swift中有很多工具可以简化JSON的工作。我建议您特别关注EVReflection和Gloss,尽管也有很多人使用swift json。

You don't mention how you're accessing the network; you could look at AFNetworking or Alamofire. The latter also has AlamofireJsonToObjects to help.

你没有提到你是如何访问网络的;你可以看看AFNetworking或Alamofire。后者也有AlamofireJsonToObjects提供帮助。

I also found JSONExport to be incredibly useful.

我还发现JSONExport非常有用。

You'd be spending a lot less time futzing with details as in this unfortunate question and more getting on with your larger goal.

在这个不幸的问题上,你会花更少的时间来处理细节问题,而更多的是为了实现更大的目标。