Sort NSDictionary in ascending order

前端 未结 3 1596
孤城傲影
孤城傲影 2021-01-28 02:55

I am trying to sort an NSDictionary in ascending order. I am using this code:

NSDictionary *valDict = self.mGetDataDict[key][rowKey];

for (NSString         


        
3条回答
  •  北海茫月
    2021-01-28 03:31

    You can use NSSortDescriptor like this:

    NSArray* array1 = @[@"1 = off", @"10 = off", @"2 = on", @"3 = on"];
    NSSortDescriptor *descriptor = [NSSortDescriptor sortDescriptorWithKey:@"" ascending:YES selector:@selector(localizedStandardCompare:)];
    
    NSLog(@"Ordered array: %@", [array1 sortedArrayUsingDescriptors:@[ descriptor ]]);
    

    which produces this output:

    2013-06-04 12:26:22.039 EcoverdFira[3693:c07] Ordered array: (
      "1 = off",
      "2 = on",
      "3 = on",
      "10 = off"
    )
    

    There's a good article on NSSortedDescriptor's here.

提交回复
热议问题