使用特定键对 NSArray 进行排序
Posted
技术标签:
【中文标题】使用特定键对 NSArray 进行排序【英文标题】:Sort NSArray with a specific key 【发布时间】:2017-03-30 06:37:09 【问题描述】:我已经从服务器请求了一个数组,现在我想过滤所有在一个新数组中具有"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];
【问题讨论】:
@Anbu.Karthik 我已经更新了我的代码。 【参考方案1】: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);
【讨论】:
【参考方案2】:NSArray *arr = [result_dic valueForKey:@"data"];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K == %@",@"status_read",@"1"];
NSArray *filteredArray = [arr filteredArrayUsingPredicate:predicate];
NSLog(@"Filtered Array: %@",filteredArray);
【讨论】:
【参考方案3】: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);
数组不会改变。
【讨论】:
以上是关于使用特定键对 NSArray 进行排序的主要内容,如果未能解决你的问题,请参考以下文章