码迷,mamicode.com
首页 > 编程语言 > 详细

对时间进行排序

时间:2015-09-17 15:31:29      阅读:188      评论:0      收藏:0      [点我收藏+]

标签:时间排序

 //排序 对时间进行排序
   NSMutableArray *dataArray=[[NSMutableArray alloc]initWithCapacity:0];
    NSMutableDictionary *dir=[[NSMutableDictionary alloc]init];
    [dir setObject:@"2012-12-02" forKey:@"time"];
    [dataArray addObject:dir];
    [dir release];
    
    NSMutableDictionary *dir1=[[NSMutableDictionary alloc]init];
    [dir1 setObject:@"2012-02-15" forKey:@"time"];
    [dataArray addObject:dir1];
    [dir1 release];
    
    NSMutableDictionary *dir2=[[NSMutableDictionary alloc]init];
    [dir2 setObject:@"2012-03-02" forKey:@"time"];
    [dataArray addObject:dir2];
    [dir2 release];
    
    NSMutableDictionary *dir3=[[NSMutableDictionary alloc]init];
    [dir3 setObject:@"2000-03-02" forKey:@"time"];
    [dataArray addObject:dir3];
    [dir3 release];
    
    NSMutableDictionary *dir4=[[NSMutableDictionary alloc]init];
    [dir4 setObject:@"2112-03-02" forKey:@"time"];
    [dataArray addObject:dir4];
    [dir4 release];
    
    NSMutableArray *myArray=[[NSMutableArray alloc]initWithCapacity:0];
    [myArray addObjectsFromArray:dataArray];
    //[myArray release];
    [dataArray release];
    
    for (int i=0; i<[myArray count]; i++) {
        NSLog(@"排序前----->>%@",[[myArray objectAtIndex:i] objectForKey:@"time"]);
    }
    
    NSSortDescriptor*sorter=[[NSSortDescriptor alloc]initWithKey:@"time" ascending:YES];
    NSMutableArray *sortDescriptors=[[NSMutableArray alloc]initWithObjects:&sorter count:1];
    NSArray *sortArray=[myArray sortedArrayUsingDescriptors:sortDescriptors];
    [sorter release];
    
    
    for (int i=0; i<[sortArray count]; i++) {
        NSLog(@"排序后----->>%@",[[sortArray objectAtIndex:i] objectForKey:@"time"]);
    }
    

本文出自 “Socket” 博客,请务必保留此出处http://6951916.blog.51cto.com/6941916/1695556

对时间进行排序

标签:时间排序

原文地址:http://6951916.blog.51cto.com/6941916/1695556

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!