NSSortDescriptorクラス
解説
継承 | NSObject |
準拠 | NSObject (NSObject) |
フレームワーク | /System/Library/Frameworks/Foundation.framework |
使用可能 | OS X 10.3以降 |
iOS 2.0以降 | |
定義 | NSSortDescriptor.h |
概要
オブジェクトの並べ替えのためのルールを定義するクラスです。
作成
ソートデスクリプタの作成
キーと並び順でソートデスクリプタを作成するには(+ sortDescriptorWithKey:ascending)メソッドを使います。ソートする際に使う比較メソッドを指定したい場合には(+ sortDescriptorWithKey:ascending:selector)メソッドを使います。ソートする際にオブジェクトの比較をブロックで行う場合は(+ sortDescriptorWithKey:ascending:comparator)メソッドを使います。
selectorを使う場合
#pragma mark NSSortDescriptor sortDescriptorWithKey:ascending: -(void)method006 { NSArray *anArray = [NSArray arrayWithObjects:@"aaa",@"cccc",@"bbb",@"a",nil]; NSArray *descs = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"length" ascending:YES selector:@selector(compare:)]]; NSLog( @"%s %@",__FUNCTION__,[anArray sortedArrayUsingDescriptors:descs] ); //=>(a,aaa,bbb,cccc) //NSString+Extractとしてカテゴリを作っている。comparePlus:というメソッドを作成済み。 //これは単に文字の長さが長いかを比べるメソッド NSArray *descs2 = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"description" ascending:YES selector:@selector(compareLength:)]]; NSLog( @"%s %@",__FUNCTION__,[anArray sortedArrayUsingDescriptors:descs2] ); //=>(cccc,aaa,bbb,a) }
comparatorを使う場合
#pragma mark NSSortDescriptor sortDescriptorWithKey:ascending: -(void)method007 { NSArray *anArray = [NSArray arrayWithObjects:@"aaa",@"cccc",@"bbb",@"a",nil]; NSArray *descs = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"length" ascending:YES selector:@selector(compare:)]]; NSLog( @"%s %@",__FUNCTION__,[anArray sortedArrayUsingDescriptors:descs] ); //=>(a,aaa,bbb,cccc) NSArray *descs2 = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"description" ascending:YES comparator:^(id obj1, id obj2) { return (NSComparisonResult)[obj1 compare:obj2]; }]]; NSLog( @"%s %@",__FUNCTION__,[anArray sortedArrayUsingDescriptors:descs2] ); //=>(a,aaa,bbb,cccc) }
allocで作成後、初期化をする方法もあります。上記メソッドと同様の初期化メソッドです。(– initWithKey:ascending)(– initWithKey:ascending:selector)(– initWithKey:ascending:comparator)
ソートデスクリプタについての情報
ソートデスクリプタが昇順か降順かを知るには(– ascending)メソッドを使います。昇順であればYESが返ります。ソートデスクリプタのソートキーを得るには(– key)メソッドを使います。ソートデスクリプタのセレクタを知りたい場合は(– selector)メソッドを使います。
ソートデスクリプタのNSComparatorを知りたい場合は(– comparator)メソッドを使います。
セレクタを得る
#pragma mark NSSortDescriptor selector -(void)method010 { NSArray *anArray = [NSArray arrayWithObjects:@"aaaaa",@"aaa",@"a",@"aa",@"aaaa",nil]; NSSortDescriptor *desc = [NSSortDescriptor sortDescriptorWithKey:@"length" ascending:YES]; NSArray *descs = [NSArray arrayWithObject:desc]; NSArray *sortedArray = [anArray sortedArrayUsingDescriptors:descs] ; NSLog( @"%s %@ %@",__FUNCTION__,NSStringFromSelector([desc selector]),sortedArray ); }
ソートデスクリプタを使う
ソートデスクリプタを使ってオブジェクトの比較をするには(– compareObject:toObject)メソッドを使用します。
逆順の新しいソートデスクリプタを返すには(– reversedSortDescriptor)メソッドを作ります。
適合するプロトコル
メソッド
ソートデスクリプタの初期化
+ sortDescriptorWithKey:ascending
– initWithKey:ascending
+ sortDescriptorWithKey:ascending:selector
– initWithKey:ascending:selector
+ sortDescriptorWithKey:ascending:comparator
– initWithKey:ascending:comparator
ソートデスクリプタについての情報を得る
ソートデスクリプタを使う
ソートデスクリプタのためのNSComparatorを作成
サブクラス化の注意