NSComparator
cmptr = ^(
id
obj1,
id
obj2) {
int
dicOne;
int dicTwo;
if ([[obj1 objectForKey : @"user_id" ] isEqual :[ NSNull null ]]) {
dicOne = 0;
} else {
dicOne = [[obj1 objectForKey : @"user_id" ] intValue ];
}
if ([[obj2 objectForKey : @"user_id" ] isEqual :[ NSNull null ]]) {
dicTwo = 0;
} else {
dicTwo = [[obj2 objectForKey : @"user_id" ] intValue ];
}
if (dicOne > dicTwo) {
return ( NSComparisonResult ) NSOrderedDescending ;
}
if (dicOne < dicTwo) {
return ( NSComparisonResult ) NSOrderedAscending ;
}
return ( NSComparisonResult ) NSOrderedSame ;
};
int dicTwo;
if ([[obj1 objectForKey : @"user_id" ] isEqual :[ NSNull null ]]) {
dicOne = 0;
} else {
dicOne = [[obj1 objectForKey : @"user_id" ] intValue ];
}
if ([[obj2 objectForKey : @"user_id" ] isEqual :[ NSNull null ]]) {
dicTwo = 0;
} else {
dicTwo = [[obj2 objectForKey : @"user_id" ] intValue ];
}
if (dicOne > dicTwo) {
return ( NSComparisonResult ) NSOrderedDescending ;
}
if (dicOne < dicTwo) {
return ( NSComparisonResult ) NSOrderedAscending ;
}
return ( NSComparisonResult ) NSOrderedSame ;
};
tempArr = [tempArr sortedArrayUsingComparator:cmptr];
//以上是tempArr由大到小排序,其中user_id为object中的key,也就是根据user_id大小来排序。