LoginSignup
25

More than 5 years have passed since last update.

NSDictionaryの入ったNSArrayを特定のキーでソートする

Last updated at Posted at 2012-12-26

arrayというNSArrayにNSDictionaryが入っていて、その中のkeyのvalueでソートしたいとします。
高速列挙ではソートはしてくれないため、NSSortDescriptorを使います。

//ソート対象となるキーを指定した、NSSortDescriptorの生成
NSSortDescriptor *sortDescNumber;
sortDescNumber = [[NSSortDescriptor alloc] initWithKey:@"key" ascending:YES];   
// NSSortDescriptorは配列に入れてNSArrayに渡す
NSArray *sortDescArray;
sortDescArray = [NSArray arrayWithObjects:sortDescNumber, nil];
// ソートの実行
NSArray *sortArray;
sortArray = [array sortedArrayUsingDescriptors:sortDescArray];
// 以上、ソート完了

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
25