通过Array中的Array对JSON数组进行排序

时间:2015-04-19 15:38:42

标签: objective-c

我需要通过数组中的数组对json进行排序(希望这是有意义的)。在下面的JSON中,我需要按距离/里程对所有项目进行排序。所以我的代码如下,但我注意到了崩溃。

NSArray *beforeSortArray = [jsonDictionary objectForKey:@"items"];

NSSortDescriptor *distanceSortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"distance.miles"
                                                                                 ascending:YES
                                                                                  selector:@selector(localizedStandardCompare:)];
        _arrAllKeys = [beforeSortArray sortedArrayUsingDescriptors:@[distanceSortDescriptor]];//CRASHES HERE

我的JSON片段:

{
"items": [
    {
        "ID": "794",
        "title": "Here is a title",
        "price": "450.00",
        "date_time": "2015-04-18 15:24:23",
        "description": "Here is description",
        "category_id": "8",
        "distance": {
            "miles": 6465.5731978902,
            "kilometers": 10405.331432585
        },
        "images": [
            "http://myimageurl.com"
        ],
        "latitude": "00.00000",
        "longitude": "-00.0000",
        "location": "Kansas City KS",
        "creationdate": "Posted: Apr 18, 2015",
        "sellerUserID": "393",
        "seller_email": "email@email.com",
        "is_deleted": "0"
    },

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

在发布答案的人的帮助下,后来将其删除了,这非常有帮助。我不确定为什么它会被删除,因为它让我走上正轨。如果你看到这个,谢谢你。我设法使用以下内容:

         _arrAllKeys2 = [jsonDictionary objectForKey:@"items"];

        NSSortDescriptor *distanceSortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"distance.miles" ascending:YES];

        _arrAllKeys = [_arrAllKeys2 sortedArrayUsingDescriptors:@[distanceSortDescriptor]];