initWithKey:ascending:
キーと昇降順でソートデスクリプタを初期化して返します
-(id)initWithKey:(NSString *)key: ascending:(BOOL)ascending:
解説
キーと昇降順でソートデスクリプタを初期化して返します。
返り値
( id )
オブジェクト(ソートデスクリプタ)
引数
( NSString * )key
( BOOL )ascending
フレームワーク
Foundation
クラス
NSSortDescriptor
Instance Methods
使用可能
10.3
参照
-initWithKey:ascending:selector:
例文
NSDictionaryをソートする
#pragma mark initWithKey: -(void)method001 { NSMutableArray *arr = [NSMutableArray arrayWithCapacity:0]; NSMutableDictionary *dic1 = [NSMutableDictionary dictionaryWithObjectsAndKeys: @"aaa4",@"key1",@"bbb1",@"key2",@"ccc1",@"key3",nil]; NSMutableDictionary *dic2 = [NSMutableDictionary dictionaryWithObjectsAndKeys: @"aaa2",@"key1",@"bbb2",@"key2",@"ccc2",@"key3",nil]; NSMutableDictionary *dic3 = [NSMutableDictionary dictionaryWithObjectsAndKeys: @"aaa1",@"key1",@"bbb3",@"key2",@"ccc3",@"key3",nil]; NSMutableDictionary *dic4 = [NSMutableDictionary dictionaryWithObjectsAndKeys: @"aaa3",@"key1",@"bbb4",@"key2",@"ccc4",@"key3",nil]; [arr addObject: dic1 ]; [arr addObject: dic2 ]; [arr addObject: dic3 ]; [arr addObject: dic4 ]; NSSortDescriptor *descriptor=[[NSSortDescriptor alloc] initWithKey:@"key1" ascending:YES]; NSArray *sortedArray = [arr sortedArrayUsingDescriptors:[NSArray arrayWithObject:descriptor]] ; NSLog(@"%s %@,%@,%@,%@",__FUNCTION__,[[arr objectAtIndex:0] objectForKey:@"key1"], [[arr objectAtIndex:1] objectForKey:@"key1"], [[arr objectAtIndex:2] objectForKey:@"key1"], [[arr objectAtIndex:3] objectForKey:@"key1"]); //=>-[OOOAppDelegate method001] aaa4,aaa2,aaa1,aaa3 NSLog(@"%s %@,%@,%@,%@",__FUNCTION__,[[sortedArray objectAtIndex:0] objectForKey:@"key1"], [[sortedArray objectAtIndex:1] objectForKey:@"key1"], [[sortedArray objectAtIndex:2] objectForKey:@"key1"], [[sortedArray objectAtIndex:3] objectForKey:@"key1"]); //=>-[OOOAppDelegate method001] aaa1,aaa2,aaa3,aaa4 }
クラスをソートする
#pragma mark initWithKey: //クラスをソートする /* //CustomClass.h #import <UIKit/UIKit.h> @interface CustomClass : NSObject { NSString *customClassValue; } @property (retain) NSString *customClassValue; @end //CustomClass.m #import "CustomClass.h" @implementation CustomClass @synthesize customClassValue; -(NSString *)description { return customClassValue; } @end */ -(void)method002 { NSMutableArray *arr = [NSMutableArray arrayWithCapacity:0]; CustomClass *obj1 = [[CustomClass alloc] init]; [obj1 setCustomClassValue:@"aaa4"]; CustomClass *obj2 = [[CustomClass alloc] init]; [obj2 setCustomClassValue:@"aaa2"]; CustomClass *obj3 = [[CustomClass alloc] init]; [obj3 setCustomClassValue:@"aaa1"]; CustomClass *obj4 = [[CustomClass alloc] init]; [obj4 setCustomClassValue:@"aaa3"]; [arr addObject: obj1 ]; [arr addObject: obj2 ]; [arr addObject: obj3 ]; [arr addObject: obj4 ]; NSSortDescriptor *descriptor=[[NSSortDescriptor alloc] initWithKey:@"customClassValue" ascending:YES]; NSArray *sortedArray = [arr sortedArrayUsingDescriptors:[NSArray arrayWithObject:descriptor]] ; NSLog(@"%s %@",__FUNCTION__,[arr description]); //=>-[OOOAppDelegate method002] (aaa4,aaa2,aaa1,aaa3) NSLog(@"%s %@",__FUNCTION__,[sortedArray description]); //=>-[OOOAppDelegate method002] (aaa1,aaa2,aaa3,aaa4) }