How to sort an array which contains Dictionaries?

前端 未结 5 403
Happy的楠姐
Happy的楠姐 2020-12-30 20:05

I have an array which contains dictionaries in it; so how can I sort the array according to dictionary key values?

5条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-30 20:39

    If every element in the array is a dictionary containing a certain key, you can sort the array using a sort descriptor. For instance, if every element is a dictionary containing a key called "name":

    NSSortDescriptor *sortByName = [NSSortDescriptor sortDescriptorWithKey:@"name"
        ascending:YES];
    NSArray *sortDescriptors = [NSArray arrayWithObject:sortByName];
    NSArray *sortedArray = [array sortedArrayUsingDescriptors:sortDescriptors];
    

提交回复
热议问题