开发者

sort NSDictionary values by key alphabetical order

开发者 https://www.devze.com 2023-04-10 07:38 出处:网络
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

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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消