首页 > 代码库 > 复杂对象数组的排序

复杂对象数组的排序

 

(注:result为待排序数组,newResult为排序后的数组)

 

NSArray *newResult =

        [result sortedArrayUsingComparator:^(id obj1,id obj2)

        {

            NSDictionary *dic1 = (NSDictionary *)obj1;

            NSDictionary *dic2 = (NSDictionary *)obj2;

            NSNumber *num1 = (NSNumber *)[dic1 objectForKey:@"value"];

            NSNumber *num2 = (NSNumber *)[dic2 objectForKey:@"value"];

            if ([num1 floatValue] > [num2 floatValue])

            {

                return (NSComparisonResult)NSOrderedAscending;

            }

            else

            {

                return (NSComparisonResult)NSOrderedDescending;

            }

            return (NSComparisonResult)NSOrderedSame;

        }];