按键字母顺序排序NSDictionary值

时间:2021-10-29 13:45:44

I can get an array of dictionary keys sorted by values, but how to I get an array of values sorted by dictionary keys? I've been looking everywhere with no luck. Any help appreciated.

我可以获取按值排序的字典键数组,但是如何获取按字典键排序的值数组?我一直在寻找没有运气的地方。任何帮助赞赏。

4 个解决方案

#1


44  

This might work:

这可能有效:

NSArray * sortedKeys = [[dict allKeys] sortedArrayUsingSelector: @selector(caseInsensitiveCompare:)];

NSArray * objects = [dict objectsForKeys: sortedKeys notFoundMarker: [NSNull null]];

or in Swift

或者在斯威夫特

let objects = dict.keys.sorted().flatMap{ dict[$0] }

or

要么

let objects = dict.sorted{ $0.0 < $1.0 }.map{ $1 }

…and that is why you should start developing in Swift ????

......这就是为什么你应该开始在Swift开发????

#2


2  

One way is to construct the array of sorted dictionary keys, then create another array of the values based on the array of keys:

一种方法是构造排序字典键数组,然后根据键数组创建另一个值数组:

//Construct array of sorted keys
NSArray keyarray = ... //etc

NSMutableArray valuearray = [[NSMutableArray alloc] init];
for (id key in keyarray) {
    [valuearray addObject:[dict objectForKey:key]];
}

#3


1  

My experience shows that sorting NSDictionary by keys is not much useful. Although after logging the NSDictionary they seem to be sorted, when needed to put them in a tableview they are not in order any more.

我的经验表明,按键排序NSDictionary并没有太大用处。虽然在记录NSDictionary之后它们似乎已被排序,但是当需要将它们放在tableview中时它们不再有序。

I suggest storing the keys in an NSArray property and then query the NSDictionary objects according to the order of the keys in the NSArray.

我建议将密钥存储在NSArray属性中,然后根据NSArray中键的顺序查询NSDictionary对象。

#4


0  

I created a category on NSDictionary to get this accomplished:

我在NSDictionary上创建了一个类别来完成这个:

@implementation NSDictionary (Extra)

-(NSArray *) sortedKeys {
    return [[self allKeys] sortedArrayUsingSelector:@selector(compare:)];
}

-(NSArray *) allValuesSortedByKey {
    return [self objectsForKeys:self.sortedKeys notFoundMarker:[NSNull null]];
}

-(id) firstKey {
    return [self.sortedKeys firstObject];
}

-(id) firstValue {
    return [self valueForKey: [self firstKey]];
}

@end

#1


44  

This might work:

这可能有效:

NSArray * sortedKeys = [[dict allKeys] sortedArrayUsingSelector: @selector(caseInsensitiveCompare:)];

NSArray * objects = [dict objectsForKeys: sortedKeys notFoundMarker: [NSNull null]];

or in Swift

或者在斯威夫特

let objects = dict.keys.sorted().flatMap{ dict[$0] }

or

要么

let objects = dict.sorted{ $0.0 < $1.0 }.map{ $1 }

…and that is why you should start developing in Swift ????

......这就是为什么你应该开始在Swift开发????

#2


2  

One way is to construct the array of sorted dictionary keys, then create another array of the values based on the array of keys:

一种方法是构造排序字典键数组,然后根据键数组创建另一个值数组:

//Construct array of sorted keys
NSArray keyarray = ... //etc

NSMutableArray valuearray = [[NSMutableArray alloc] init];
for (id key in keyarray) {
    [valuearray addObject:[dict objectForKey:key]];
}

#3


1  

My experience shows that sorting NSDictionary by keys is not much useful. Although after logging the NSDictionary they seem to be sorted, when needed to put them in a tableview they are not in order any more.

我的经验表明,按键排序NSDictionary并没有太大用处。虽然在记录NSDictionary之后它们似乎已被排序,但是当需要将它们放在tableview中时它们不再有序。

I suggest storing the keys in an NSArray property and then query the NSDictionary objects according to the order of the keys in the NSArray.

我建议将密钥存储在NSArray属性中,然后根据NSArray中键的顺序查询NSDictionary对象。

#4


0  

I created a category on NSDictionary to get this accomplished:

我在NSDictionary上创建了一个类别来完成这个:

@implementation NSDictionary (Extra)

-(NSArray *) sortedKeys {
    return [[self allKeys] sortedArrayUsingSelector:@selector(compare:)];
}

-(NSArray *) allValuesSortedByKey {
    return [self objectsForKeys:self.sortedKeys notFoundMarker:[NSNull null]];
}

-(id) firstKey {
    return [self.sortedKeys firstObject];
}

-(id) firstValue {
    return [self valueForKey: [self firstKey]];
}

@end