OC 冒泡排序 ——时间排序

来源:互联网 发布:python调用caffemodel 编辑:程序博客网 时间:2024/05/19 17:25

    NSMutableArray * array = [NSMutableArrayarrayWithObjects:

                       @"2013-10-18",

                       @"2013-10-11",

                       @"2013-10-16",

                       @"2013-10-15",

                       @"2013-10-14",

                       @"2013-10-13",

                       @"2013-10-19",

                       @"2013-10-12", nil];

    NSLog(@"排序前:%@",array);

    

    int a = [arraycount];

    NSDateFormatter *dateFormatter = [[NSDateFormatteralloc] init];

    [dateFormatter setDateFormat:@"yyyy-MM-dd"];

    NSLog(@"%@",array);

    NSString * tempStr = [NSStringstring];

    

    //冒泡排序

    for (int i =0; i<a-1; i++) {

        for (int j =0; j<a-i-1; j++) {

            NSDate * date1 = [dateFormatterdateFromString:[array objectAtIndex:j+1]];

            NSDate * date2 = [dateFormatterdateFromString:[array objectAtIndex:j]];

            if (date1==[date1earlierDate:date2]) {

                tempStr = [arrayobjectAtIndex:j+1];

                [arrayexchangeObjectAtIndex:j+1withObjectAtIndex:j];

                [array replaceObjectAtIndex:j withObject:tempStr];

            }

        }

    }

    

    NSLog(@"排序后:%@",array);


运行环境:Xcode5.0

连接:http://download.csdn.net/detail/u012405234/6387339

原创粉丝点击