1

我致力于在表视图中显示 json 数据并将表视图数据传递给下一个视图控制器。在这里,我想根据名称对我的 json 响应进行排序,然后在表格视图中显示。这是我的 json 数据

"data": [
    {
        "_id": "597323707c691a6b27c63bcc",
        "name": "Adidas",
        "file": "http://104.236.67.117:5000/uploads/1500717927565.jpg",
        "logo": "http://104.236.67.117:5000/uploads/1500717910702.jpg",     
    },
    {
        "_id": "597323707c691a6b27c63bcc",
        "name": "Bibo",
        "file": "http://104.236.67.117:5000/uploads/1500717927565.jpg",
        "logo": "http://104.236.67.117:5000/uploads/1500717910702.jpg",     
    }, {
        "_id": "597323707c691a6b27c63bcc",
        "name": "Caprese",
        "file": "http://104.236.67.117:5000/uploads/1500717927565.jpg",
        "logo": "http://104.236.67.117:5000/uploads/1500717910702.jpg",     
    }, {
        "_id": "597323707c691a6b27c63bcc",
        "name": "DK",
        "file": "http://104.236.67.117:5000/uploads/1500717927565.jpg",
        "logo": "http://104.236.67.117:5000/uploads/1500717910702.jpg",     
    }

我只能对名称进行排序,但不能对整个对象进行排序。任何人都可以为我解决这个问题吗?这对我很有帮助......在此先感谢......

4

3 回答 3

1
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"Name" ascending:YES selector:@selector(caseInsensitiveCompare:)];
    [arr sortUsingDescriptors:@[sortDescriptor]];

将其更改为快速代码。它的工作。我用 NSDictionaries 检查了这段代码。

于 2017-08-17T08:06:49.353 回答
0

在目标 C 中:(user3306145 的答案)

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc]
                                    initWithKey: @"name" ascending: YES];

NSArray *sortedArray = [self.displayItems sortedArrayUsingDescriptors: [NSArray arrayWithObject:sortDescriptor]];
于 2017-08-17T07:48:04.513 回答
-1

var 描述符:NSSortDescriptor = NSSortDescriptor(键:“名称”,升序:true) var sortedResults:NSArray = results.sortedArrayUsingDescriptors([descriptor])

于 2017-08-17T07:32:51.037 回答