简体   繁体   中英

Sort NSDictionary in ascending order

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

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

for (NSString *valueKey in
     [[valDict allKeys] sortedArrayUsingSelector:@selector(compare:)])
{                
    if ([valueKey isEqualToString:@"attr"])
    {
        dictRow = self.mGetDataDict[key][rowKey][valueKey];
    }
    else {
        NSString *valKey = self.mGetDataDict[key][rowKey][valueKey];
        [arrSeatsStatus addObject:valKey];
    }
}

This is the output I'm getting:

1 = off;
10 = off;
2 = on;
3 = on;
4 = on;
5 = on;
6 = on;
7 = on;
8 = on;
9 = on;

This is the required output:

1: "off",
2: "on",
3: "on",
4: "on",
5: "on",
6: "on",
7: "on",
8: "on",
9: "on",
10: "off"

The required output is an actual value coming from JSON.

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 .

Try to use this one....

// changes.......

NSArray *valDict = [[self.mGetDataDict allValues] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
NSMutableDictionary *orderedDictionary=[[NSMutableDictionary alloc] init];

for (NSString *valor in valDict)
{
    for (NSString *clave in [yourDictionary allKeys])
    {
        if ([valor isEqualToString:[valDict valueForKey:clave]]) 
        {
            [orderedDictionary setValue:valor forKey:clave];
        }
    }
}

Get the Array of the Values, sort that array and then get the key corresponding to the value.

You can get the values with:

NSArray* values = [myDict allValues];
NSArray* sortedValues = [values sortedArrayUsingSelector:@selector(comparator)];

But, if the collection is as you show in your example, (I mean, you can infer the value from the key), you can always sort the keys instead messing with the values.

Using:

NSArray* sortedKeys = [myDict keysSortedByValueUsingSelector:@selector(comparator)];

The comparator is a message selector which is sent to the object you want to order.

If you want to order strings, then you should use a NSString comparator. The NSString comparators are ie: caseInsensitiveCompare or localizedCaseInsensitiveCompare:.

If none of these are valid for you, you can call your own comparator function

[values sortedArrayUsingFunction:comparatorFunction context:nil]

Being comparatorFunction (from AppleDocumentation )

NSInteger intSort(id num1, id num2, void *context)
{
    int v1 = [num1 intValue];
    int v2 = [num2 intValue];
    if (v1 < v2)
        return NSOrderedAscending;
    else if (v1 > v2)
        return NSOrderedDescending;
    else
        return NSOrderedSame;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM