ios object-c selfdataSource 根据数组里面imgCode的大小进行排序小的放前面
要根据数组里面imgCode的大小进行排序,可以使用以下代码:
NSArray *sortedArray = [self.dataSource sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
NSDictionary *dict1 = (NSDictionary *)obj1;
NSDictionary *dict2 = (NSDictionary *)obj2;
NSInteger imgCode1 = [[dict1 objectForKey:@"imgCode"] integerValue];
NSInteger imgCode2 = [[dict2 objectForKey:@"imgCode"] integerValue];
if (imgCode1 < imgCode2) {
return NSOrderedAscending;
} else if (imgCode1 > imgCode2) {
return NSOrderedDescending;
} else {
return NSOrderedSame;
}
}];
self.dataSource = [NSMutableArray arrayWithArray:sortedArray];
这段代码使用了sortedArrayUsingComparator:方法来进行排序。在比较器中,首先获取每个字典中的imgCode值,然后根据大小返回不同的NSComparisonResult值。最后,将排序后的数组赋值给self.dataSource
原文地址: https://www.cveoy.top/t/topic/iBuU 著作权归作者所有。请勿转载和采集!