数组里面的(字典 对象)排序和对比 去重复
01.(可变数组)
[self.tableview.dataArray addObjectsFromArray:self.liveCommentList];
[parray addObjectsFromArray:self.tableview.dataArray];
[parray sortUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) {
ZhiBoJianModel * dicum1 =obj1;
ZhiBoJianModel * dicum2 =obj2;
if (([dicum1.comment isEqualToString:dicum2.comment]==YES) &&([dicum1.Id isEqualToString:dicum2.Id]==YES)) {
[self.tableview.dataArray removeObject:dicum1];
}
return YES;
}];
02.不可变数组
// NSArray * stortedarray3 =[arr1 sortedArrayUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) {
// NSDictionary * dicum1 =obj1;
// NSDictionary * dicum2 =obj2;
//
// if ([dicum1[@"age"] isEqualToString:dicum2[@"age"]]==YES) {
//
// [parray removeObject:dicum1];
//
//
// }
//
// //升序
// // NSComparisonResult result =[obj1 compare:obj2];
//
// //降序
// // NSComparisonResult result =[obj2 compare:obj1];
//
// return YES;
//
// }];
NSArray *array = [NSArray arrayWithObjects:@"75",@"23",@"43",@"12",@"02", nil];
//普通排序系统自带的升序
NSArray *sortedArray1 = [array sortedArrayUsingSelector:@selector(compare:)];
// NSLog(@"排序后1:%@",sortedArray1);
//当然先这样实现了升序,但你想要降序,怎么办?
//倒序输出 -> 得到降序数组
sortedArray1 = [[sortedArray1 reverseObjectEnumerator] allObjects];
NSLog(@"降序 = %@",sortedArray1);
没有带(字典(对象))的数组对比
// NSArray * arr1 = @[@1,@2,@3];
//
// NSArray * arr2 = @[@2,@3,@4,@5];
//
// NSPredicate * filterPredicate = [NSPredicate predicateWithFormat:@"NOT (SELF IN %@)",arr1];
//
// NSArray * filter = [arr2 filteredArrayUsingPredicate:filterPredicate];
// NSLog(@"%@",filter);
NSArray * stortedarray3 =[array sortedArrayUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) {
//升序
NSComparisonResult result =[obj1 compare:obj2];
//降序
// NSComparisonResult result =[obj2 compare:obj1];
return result;
}];
- 高级排序:按描述进行排序(制定一套排序规则 )
NSMutableDictionary * dic =[NSMutableDictionary dictionary];
dic[@"name"] =@"ahao";
dic[@"age"] =@"17";
NSMutableDictionary * dic12 =[NSMutableDictionary dictionary];
dic12[@"name"] =@"yu";
dic12[@"age"] =@"50";
NSMutableDictionary * dic23 =[NSMutableDictionary dictionary];
dic23[@"name"] =@"chong";
dic23[@"age"] =@"30";
NSArray *array = [NSArray arrayWithObjects:dic,dic12,dic23, nil];
NSSortDescriptor * re =[NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES];
NSSortDescriptor * re1 =[NSSortDescriptor sortDescriptorWithKey:@"age" ascending:YES];
//按年龄排序
NSArray * dearray =[NSArray arrayWithObjects:re1,re, nil];
//(升序)
NSArray * dearray1 =[array sortedArrayUsingDescriptors:dearray];
//降序
NSArray * sortedArray1 = [[dearray1 reverseObjectEnumerator] allObjects];
NSLog(@"降序%@",sortedArray1);
NSLog(@"排序%@",dearray1);
4 字典排序
对字典(Key-Value)排序 不区分大小写
@param dict 要排序的字典
*/
- (void)sortedDictionarybyLowercaseString:(NSDictionary *)dict{
//将所有的key放进数组
NSArray *allKeyArray = [dict allKeys];
//序列化器对数组进行排序的block 返回值为排序后的数组
NSArray *afterSortKeyArray = [allKeyArray sortedArrayUsingComparator:^NSComparisonResult(id _Nonnull obj1, id
Nonnull obj2) {
/**
- (NSComparisonResult)compare:(NSString *)string;
compare方法的比较原理为,依次比较当前字符串的第一个字母:
如果不同,按照输出排序结果
如果相同,依次比较当前字符串的下一个字母(这里是第二个)
以此类推
排序结果
NSComparisonResult resuest = [obj1 compare:obj2];为从小到大,即升序;
NSComparisonResult resuest = [obj2 compare:obj1];为从大到小,即降序;
注意:compare方法是区分大小写的,即按照ASCII排序
*/
//小写转化
obj1 = [obj1 lowercaseString];
obj2 = [obj2 lowercaseString];
//排序操作
NSComparisonResult resuest = [obj1 compare:obj2];
return resuest;
}];
NSLog(@"afterSortKeyArray:%@",afterSortKeyArray);
//通过排列的key值获取value
NSMutableArray *valueArray = [NSMutableArray array];
for (NSString *sortsing in afterSortKeyArray) {
NSString *valueString = [dict objectForKey:sortsing];
[valueArray addObject:valueString];
}
NSLog(@"valueArray:%@",valueArray);
}
5 简单测试代码
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:@"Danny",@"Basd",@"12",@"Cerff",@"man",@"ABCDEFG",@"Sean",@"Cerf",nil];
NSLog(@"dictionary:%@",dict);
[self sortedDictionary:dict];
网友评论