使用特定键对NSArray进行排序

时间:2017-03-30 06:37:09

标签: ios objective-c nsarray

我已经从服务器请求了一个数组,现在我想过滤所有在新数组中有"status_read": 1的数据。我该怎么做?这是我的数据回复:

{
  "status": "success",
  "message": "success",
  "data": [
    {
      "logo": "http://**.com/timepicker/images/default_o.jpg",
      "title": "Title 1",
      "icon": "https://**.com/admin/dashboard/dist/img/a.png",
      "type": 1,
      "link_detail": "",
      "id_call": 12,
      "status_read": 1
    },
    {
      "logo": "http://**.com/timepicker/images/default_go.jpg",
      "title": "Title 2",
      "icon": "https://**.com/admin/dashboard/dist/img/b.png",
      "type": 0,
      "link_detail": "http://google.com",
      "id_call": 0,
      "status_read": 0
    }
  ]
}

这是我的过滤器数组代码:

NSString *dictionaryKey = @"status_read";
NSInteger predicateValue = 1;
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K == %i", dictionaryKey, predicateValue];
NSArray *filteredArr = [readNotif filteredArrayUsingPredicate:predicate];
generalArray = filteredArr;
[self.notifTableView reloadData];

3 个答案:

答案 0 :(得分:2)

NSString *jsonStr = @"{\"status\":\"success\",\"message\":\"success\",\"data\":[{\"logo\":\"http://**.com/timepicker/images/default_o.jpg\",\"title\":\"Title 1\",\"icon\":\"https://**.com/admin/dashboard/dist/img/a.png\",\"type\":1,\"link_detail\":\"\",\"id_call\":12,\"status_read\":1},{\"logo\":\"http://**.com/timepicker/images/default_go.jpg\",\"title\":\"Title 2\",\"icon\":\"https://**.com/admin/dashboard/dist/img/b.png\",\"type\":0,\"link_detail\":\"http://google.com\",\"id_call\":0,\"status_read\":0}]}";

NSData *data = [jsonStr dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];

//orignal array
NSArray *array = jsonDict[@"data"];
//create predicate to filter the array
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"status_read == 1"];

NSArray *filteredArray = [array filteredArrayUsingPredicate:predicate];
NSLog(@"Filtered Array: %@",filteredArray);

答案 1 :(得分:0)

NSArray *arr = [result_dic valueForKey:@"data"];    
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K == %@",@"status_read",@"1"];

NSArray *filteredArray = [arr filteredArrayUsingPredicate:predicate];
NSLog(@"Filtered Array: %@",filteredArray);

答案 2 :(得分:0)

NSDictionary *dic1 = @{@"status_read" : @1, @"title" : @"Title 1"};
NSDictionary *dic2 = @{@"status_read" : @0, @"title" : @"Title 0"};
NSDictionary *dic5 = @{@"status_read" : @5, @"title" : @"Title 5"};
NSDictionary *dic4 = @{@"status_read" : @4, @"title" : @"Title 4"};
NSArray *array = @[dic1, dic2, dic5, dic4];

NSArray *result = [array sortedArrayUsingComparator:^NSComparisonResult(NSDictionary *_Nonnull obj1, NSDictionary *  _Nonnull obj2) {
    NSInteger objValue1 = [obj1[@"status_read"] integerValue];
    NSInteger objValue2 = [obj2[@"status_read"] integerValue];
    if (objValue1 > objValue2) {
        return (NSComparisonResult)NSOrderedDescending;
    } else if (objValue1 < objValue2) {
        return (NSComparisonResult)NSOrderedAscending;
    }
    return (NSComparisonResult)NSOrderedSame;
}];

NSLog(@"%@", result);

阵列不会改变。