相邻时间段的合并处理(数组元素排序及相邻元素分组合并)

在项目业务开发过程中遇到个需求,对用户所选择的时间点进行排序及合并处理,小记一下,作为备忘,代码如下:

\**
 \*  对时间段的相邻时段合并处理
 \*  @param timeArray 需要处理的时间段
 \*  @return 处理完的时间段
 */
+(NSMutableArray *)processSelectedTime:(NSMutableArray *)timeArray{
    NSMutableArray *selectTimeArray = [NSMutableArray array];
    for (NSString *string in timeArray) {
        [selectTimeArray addObject:[NSNumber numberWithInteger:[string integerValue]]];
    }
    [selectTimeArray sortUsingSelector:@selector(compare:)];
//合并后的时间字符串
    NSString* mergeTimeStr;
    //选择的时间段合并处理后的数组
    NSMutableArray* timeStrArray;
    //二维数组可解决这个问题
    NSMutableArray* data = [[NSMutableArray alloc]init];    
for (int i = 0; i < selectTimeArray.count; i++) {
        NSMutableArray* tmps = [[NSMutableArray alloc]init];
        [tmps addObject:selectTimeArray[i]];
        if (i == selectTimeArray.count - 1) {
            [data addObject:tmps];
            break;
        }
        int position = i;
        NSInteger tmpData = [selectTimeArray[i] integerValue] + 1;
        while (YES) {
            position++;
            if (position

你可能感兴趣的:(相邻时间段的合并处理(数组元素排序及相邻元素分组合并))