Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationEventAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
4
Help us understand the problem. What are the problem?

More than 5 years have passed since last update.

@imp954sti

NSArrayのソート

下記のようなプロパティを持ったクラスの配列をソートする

Hoge.h
@property (nonatomic) NSInteger itemId;
@property (nonatomic) NSString *name;
@property (nonatomic) NSInteger age;

Blocksを使ったソート

Fuga.m
NSArray *list = [[NSArray alloc] init];

// Hogeを適当に追加する処理
// ~

// ソート
list = [list sortedArrayUsingComparator:^(Hoge *obj1, Hoge *obj2){
    // 昇順でソートする
    return obj1.itemId - obj1.itemId;

    // 降順の場合
    // return obj2.itemId - obj1.itemId;
}];
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
4
Help us understand the problem. What are the problem?