Sort NSDictionary By property of object stored as value

前端 未结 2 898

I am storing data in NSMutableDictionary To store data received by web service.

I want to display data in alpha order but as we know Dictionary store da

相关标签:
2条回答
  • 2021-01-18 12:52
    NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:@"Object.Name" ascending:YES];
    [items sortUsingDescriptors:[NSArray arrayWithObjects:descriptor,nil]];
    

    Also,

    NSArray *ordered = [myDictionary keysSortedByValueUsingComparator:^NSComparisonResult(id obj1, id obj2){
        return [obj1.name compare:obj2.name];
    }];
    
    0 讨论(0)
  • 2021-01-18 13:15

    Try this,

    NSArray *sortedKeys = [dict keysSortedByValueUsingComparator: ^(id obj1, id obj2) {
    
         return (NSComparisonResult)[obj1.name compare:obj2.name];;
    }];
    

    You will get all the keys in sortedKeys array, sorted in the order of object.name property. You can use these keys to get the values from dictionary as [dict valueForKey:[sortedKeys objectAtIndex:i]]. For more details please check apple documentation.

    0 讨论(0)
提交回复
热议问题