Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
619 views
Welcome To Ask or Share your Answers For Others

1 Answer

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 ??


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...