美文网首页
iOS小记--NSArray 排序

iOS小记--NSArray 排序

作者: 原鸣清 | 来源:发表于2015-11-17 19:06 被阅读894次

    数组内装有V3HealthModel,依靠model的startTime属性将数组重新排序

     一:

    NSMutableArray *returnArray = [NSMutableArray array];

    NSComparator cmptr = ^(id obj1, id obj2){

    if ([((V3HealthModel *)obj1) startTime] < [((V3HealthModel *)obj2) startTime]) {

    return (NSComparisonResult)NSOrderedDescending;

    }

    else

    {

    return (NSComparisonResult)NSOrderedAscending;

    }

    };

    NSLog(@"%ld %ld",((V3HealthModel *)[healthArray firstObject]).startTime,((V3HealthModel *)[healthArray lastObject]).startTime);

    [returnArray addObjectsFromArray:[healthArray sortedArrayUsingComparator:cmptr]];

    二:

    NSArray *sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"startTime" ascending:NO]];

    [healthArray sortUsingDescriptors:sortDescriptors];

    相关文章

      网友评论

          本文标题:iOS小记--NSArray 排序

          本文链接:https://www.haomeiwen.com/subject/rvtlhttx.html