How can I sort an NSArray containing NSDictionaries?

前端 未结 3 1796
囚心锁ツ
囚心锁ツ 2020-11-27 07:49

I have an NSArray which contains NSDictionary objects. Each of these NSDictionary objects contains an ORDER key.

3条回答
  •  我在风中等你
    2020-11-27 08:28

    To Sort array of dictionaries you have to use NSSortDescriptor by this you can sort array of dictionaries based on key

    here in code, pass the NSMutableArray with dict Object by mean which you want to sort it

    -(NSMutableArray*)sortingArrayOfDict:(NSMutableArray *)arrayTosort sortByObject:(NSString*)sortKey{
    NSSortDescriptor *aSortDescriptor = [[NSSortDescriptor alloc] initWithKey:sortKey ascending:YES];
    [arrayTosort sortUsingDescriptors:[NSArray arrayWithObject:aSortDescriptor]];
    return arrayTosort;
     }
    

    or even you can sort it in ascending or descending order by setting it YES/NO

提交回复
热议问题