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

Categories

0 votes
627 views
in Technique[技术] by (71.8m points)

objective c - sort NSDictionary values by key alphabetical order

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

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

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...