【问题标题】:Objects Sorting With date ,Time Problem in Array(Iphone Development)对象按日期排序,数组中的时间问题(iPhone开发)
【发布时间】:2011-10-11 13:19:51
【问题描述】:

我遇到了与数组排序相关的问题。

我有一个 NSMutable 数组,比如 A。它的每个索引都有一个类对象 b。

class b 包含多个字段,如 int、string 和 nsdate。

我想根据b类时间(NSdate)升序对A数组进行排序。

我遵循 stackoverflow 上的日期排序问题,但这仅适用于日期数组。

Sort NSArray of date strings or objects

请指导我。

提前致谢

【问题讨论】:

    标签: iphone objective-c xcode ios4 iphone-sdk-3.0


    【解决方案1】:

    在这里,您只需根据您的要求修改部分代码

    - (NSArray *)sortedWeightEntriesByWeightDate:(NSArray *)unsortedArray {
    
        NSMutableArray *tempArray = [NSMutableArray array];
        NSMutableArray *sortedArray = [NSMutableArray arrayWithCapacity:0];
    
        @try {
            for(int i = 0; i < [unsortedArray count];i++) {
    
                NSDateFormatter *df = [[NSDateFormatter alloc]init];
                MyDataModal *entry = [unsortedArray objectAtIndex:i];       
                [df setDateFormat:@"yyyy-MM-dd"];       
                NSDate *date = [df dateFromString:entry.weightDate];        
                NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    
                if(date) {  
                    [dict setObject:entry forKey:@"entity"];        
                    [dict setObject:date forKey:@"date"];       
                    [tempArray addObject:dict];
                }
                [df release];
            }
    
            NSInteger counter = [tempArray count];
            NSDate *compareDate;
            NSInteger index;
    
            for(int i = 0 ; i < counter; i++) {
                index = i;
                compareDate = [[tempArray objectAtIndex:i] valueForKey:@"date"];        
                NSDate *compareDateSecond;
    
                for(int j = i+1 ; j < counter; j++) {
                    compareDateSecond=[[tempArray objectAtIndex:j] valueForKey:@"date"];
                    NSComparisonResult result = [compareDate compare:compareDateSecond];
                    if(result == NSOrderedDescending) {
                        compareDate = compareDateSecond;
                        index=j;
                    }
                }
                if(i!=index)
                    [tempArray exchangeObjectAtIndex:i withObjectAtIndex:index];
            }
    
    
            NSInteger counterIndex = [tempArray count];
            for(int i = 0; i < counterIndex ; i++) {
                [sortedArray addObject:[[tempArray objectAtIndex:i] valueForKey:@"entity"]];
            }
    
        }
        @catch (NSException * e) {
            NSLog(@"An exception occured while sorting weight entries by date");
        }
        @finally {
            return [NSArray arrayWithArray:sortedArray];
        }   
    }
    

    【讨论】:

    • 谢谢!我需要根据自己的需要切换到 NSOrderedAscending,但这是一个巨大的帮助
    【解决方案2】:

    怎么样:

    NSArray *myArray = [NSArray arrayWithObjects:
                        [NSDictionary dictionaryWithObjectsAndKeys:[NSDate distantFuture], @"theDate", nil],
                        [NSDictionary dictionaryWithObjectsAndKeys:[NSDate distantPast], @"theDate", nil],
                        nil];
    NSLog(@"Before sorting: %@", myArray);
    NSSortDescriptor *dateSortDescriptor = [NSSortDescriptor sortDescriptorWithKey: @"theDate" ascending: YES];
    
    NSArray *sortedArray = [myArray sortedArrayUsingDescriptors:[NSArray arrayWithObject: dateSortDescriptor]];
    NSLog(@"After Sorting: %@", sortedArray);
    

    这假定您要排序的日期有一个键(本质上它是一个属性。)

    【讨论】:

      猜你喜欢
      • 2020-10-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-16
      • 2015-09-08
      • 1970-01-01
      • 1970-01-01
      • 2016-11-05
      相关资源
      最近更新 更多