Grouping NSArray of NSDictionary based on a key in NSDictionay

前端 未结 2 1543
一向
一向 2020-12-18 02:17

I am trying to filter out a NSArray of NSDictionaries. With my below example, I want dict1, dict2 & dict4 grouped in one array, dict3 & dict5 grouped in second array

2条回答
  •  一向
    一向 (楼主)
    2020-12-18 03:02

    It's a little hard to tell if what you want is three different arrays where each one only contains entries with a specific Name value (as your first paragraph suggests) or if you want a single array where the entries are sorted by Name (as your second paragraph suggests). Regardless,

    To sort orig by the value of the Name field:

    NSArray *sortedByName = [orig sortedArrayUsingDescriptors:@[[NSSortDescriptor sortDescriptorWithKey:@"Name" ascending:YES]]];
    

    To get a new array by selecting only entries with a specific value for Name:

    NSArray *t1Only = [orig filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"Name = %@", @"T1"]];
    

提交回复
热议问题