2011-10-02 65 views
2

我有一個NSMutableArray的使用詞典,所有字典的中包含的NSDate是關鍵「日期」的NSString的形式。我想按日期排列NSDictionaries。因此,例如,我有數組的狀態如下:排序NSDictionaries通過NSDate的

Dict 
    Date 
     20.06.1996 23:30 
Dict 
    Date 
     04.10.2011 19:00 
Dict 
    Date 
     20.06.1956 23:39 

我想對它進行排序,使它看起來像這樣:

Dict 
    Date 
     20.06.1956 23:39 
Dict 
    Date 
     20.06.1996 23:30 
Dict 
    Date 
     04.10.2011 19:00 

我已經與NSSortDescriptor嘗試,但都沒有成功...

更新:

我設法日期排序,但是我來到了這個問題:在類型的字典,不僅有日期,還有其他對象,什麼我的鱈魚e是否只是切換字典之間的日期值,而不是切換完整的字典。有了這個,字典中的其他值就被分配了錯誤的日期,這非常糟糕。有誰能夠幫助我?下面是我的代碼:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"savedData.daf"]; 
NSMutableArray *d = [NSMutableArray arrayWithContentsOfFile:path]; 



for (int ii = 0; ii < [d count]; ii++) { 
    NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init]; 
    if (is24h) { 
     [dateFormatter setDateFormat:@"dd.MM.yyyy HH:mm"]; 
    } 
    else { 
     [dateFormatter setDateFormat:@"dd.MM.yyyy hh:mm a"]; 
    } 
    [dateFormatter setLocale:[NSLocale currentLocale]]; 
    NSDate *dat = [dateFormatter dateFromString:[[d valueForKey:@"Date"] objectAtIndex:ii]]; 
    NSMutableDictionary *newDict = [[NSMutableDictionary alloc] init]; 
    NSDictionary *oldDict = (NSDictionary *)[d objectAtIndex:ii]; 
    [newDict addEntriesFromDictionary:oldDict]; 
    [newDict setObject:dat forKey:@"Date"]; 
    [d replaceObjectAtIndex:ii withObject:newDict]; 
    [newDict release]; 
} 


NSSortDescriptor *sorter = [[NSSortDescriptor alloc] initWithKey:@"Date" ascending:YES]; 
NSArray *sorters = [[NSArray alloc] initWithObjects:sorter, nil]; 
[sorter release]; 
NSMutableArray *sorted = [NSMutableArray arrayWithArray:[d sortedArrayUsingDescriptors:sorters]]; 
[sorters release]; 
NSLog(@"%@",sorted); 
for (int ii = 0; ii < [sorted count]; ii++) { 
    NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init]; 
    if (is24h) { 
     [dateFormatter setDateFormat:@"dd.MM.yyyy HH:mm"]; 
    } 
    else { 
     [dateFormatter setDateFormat:@"dd.MM.yyyy hh:mm a"]; 
    } 
    [dateFormatter setLocale:[NSLocale currentLocale]]; 
    NSString *sr = [dateFormatter stringFromDate:[[sorted valueForKey:@"Date"] objectAtIndex:ii]]; 
    NSMutableDictionary *newDict = [[NSMutableDictionary alloc] init]; 
    NSDictionary *oldDict = (NSDictionary *)[d objectAtIndex:ii]; 
    [newDict addEntriesFromDictionary:oldDict]; 
    [newDict setObject:sr forKey:@"Date"]; 
    [sorted replaceObjectAtIndex:ii withObject:newDict]; 
    [newDict release]; 
} 
NSLog(@"before: %@" 
     "" 
     "after: %@",d,sorted); 
[sorted writeToFile:path atomically:YES]; 
+1

你應該張貼你不能讓工作的代碼。 – vikingosegundo

+1

我很困惑。他們是否包含NSDates或NSStrings?你說兩個,但一個對象不能同時是一個NSDate和一個NSString。 – Chuck

+0

這是NSStrings,對不起! – JonasG

回答

11

有很多方法可以做到這一點,一種方法是使用NSDate對象而不是NSStrings(或根據ISO 8601格式化的NSString,以便字典排序符合所需的排序)。然後,你可以這樣做:

NSSortDescriptor *descriptor = [NSSortDescriptor sortDescriptorWithKey:@"Date" 
                  ascending:YES]; 
[array sortUsingDescriptors:[NSArray arrayWithObject:descriptor]]; 

或者,如果你不能(或不願)改變你的數據,你可以隨時使用排序塊:

[array sortUsingComparator:^(id dict1, id dict2) { 
    NSDate *date1 = // create NSDate from dict1's Date; 
    NSDate *date2 = // create NSDate from dict2's Date; 
    return [date1 compare:date2]; 
}]; 

這當然會可能比第一種方法要慢,因爲通常最終會創建超過n NSDate對象。

+0

非常感謝,這真的幫了我,我通過另一個問題跑,它在更新的問題進行了說明,請檢查出來,並設法幫助我:) – JonasG

+0

很多不必要的代碼之外,更換'[d objectAtIndex: ii]'用'[sorted objectAtIndex:ii]'應該解決你的問題。 –

+0

歡呼!就是這樣!那麼不必要的代碼呢?:) – JonasG

0

有幾個選項,一個是將NSDate對象放在字典中。與比較字符串

的一個問題是,你可以「T只是做一個字符串比較,因爲這一年是不是在字符串的最顯著藥水。

所以,你需要編寫一個比較方法與使用方法:

- (NSArray *)sortedArrayUsingComparator:(NSComparator)cmptr 

或者是:

- (NSArray *)sortedArrayUsingFunction:(NSInteger (*)(id, id, void *))comparator context:(void *)context 

的比較將需要處理的dd.mm.yyyy hh:mm字符串格式。

其他選項包括添加具有一個NSDate表示另一字典鍵和上排序。

+1

你能給我多一些代碼嗎,我無法弄清楚這個問題! – JonasG