Sort Array of Dictionaries by NSDate

前端 未结 3 430
北海茫月
北海茫月 2021-01-01 03:05

I have an array of dictionaries. Within each dictionary, there is a a key dateOfInfo (an NSDate) and several other things. I want to sort the array

3条回答
  •  [愿得一人]
    2021-01-01 03:20

    You can sort using NSSortDescription, e.g.

    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey: @"dateOfInfo" ascending: NO];
    NSArray *sortedArray = [array sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];
    [sortDescriptor release];
    

    You can also use the method

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

提交回复
热议问题