【NSArray】ソートをする方法

Posted by: daichi  /  Category: objective-c

NSArrayのインスタンスarrayにAccountSumクラスのオブジェクトが詰められている場合、こんな感じでソートできる。

?View Code OBJECTIVE-C
array = [array sortedArrayUsingSelector:@selector(compareSum:)];



NSArrayに入れるオブジェクトにこんなメソッドを用意しておけば、あとはNSArrayがうまいことやってソートしてくれる。javaでいうComparatorみたいにソートのルールをメソッド内で定義してやる感じ。

?View Code OBJECTIVE-C
- (NSComparisonResult)compareSum:(AccountSum *)ac {
	if (self.sum > ac.sum ){
		return NSOrderedAscending;
	} else if (self.sum < ac.sum){
		return NSOrderedDescending;
	} else {
		return NSOrderedSame;
	}
}



関連のあるアプリ

islot_icon
iSlot Pro 2009/03/13 リリース
ファイナンス 350円
パチスロ収支管理アプリ
app_store_badge

タグ: iphone, iSlot, iSlot Pro, NSArray, objecti, objective-c, アプリ, パチスロ, リリース, 収支

関連する投稿

コメント

Get Adobe Flash playerPlugin by wpburn.com wordpress themes