数组排序 sort

//方法一
+ (NSArray*)getSortArrForMainApp:(NSArray*)arrSrc {
    NSArray* arrDes = [arrSrc sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
        //change your code
        NSString *value1 = obj1;
        NSString *value2 = obj2;
        return value1.intValue < value2.intValue ? NSOrderedAscending : NSOrderedDescending;
    }];

    return arrDes;
}
//方法二
//调用
        NSArray* arrChapterlistID = [pack1.iChapterIdList sortedArrayUsingFunction:chapterlistIDSort context:NULL];

//方法
static NSInteger chapterlistIDSort(id item1, id item2, void* context)
{
    NSString* chapterID1 = item1;
    NSString* chapterID2 = item2;
    NSInteger nChapterID1 = [chapterID1 integerValue];
    NSInteger nChapterID2 = [chapterID2 integerValue];
    
    NSInteger sortType = 0;
    if(nChapterID1 < nChapterID2)//cong xiao dao da pai
    {
        sortType = NSOrderedAscending;
    }
    else if (nChapterID1 == nChapterID2)
    {
        sortType = NSOrderedSame;
    }
    else
    {
        sortType = NSOrderedDescending;
    }
    return sortType;
}



 
 

你可能感兴趣的:(iOS技术)