macOS/iOS API解説

iOS , Mac アプリケーション開発のために使われる主要フレームワークの日本語情報です。2010年代に書かれた内容です。今後更新はありません。

目次

sortUsingFunction:context:

変更可能な配列を比較関数でソートします
-(void)sortUsingFunction:(NSInteger (*)(id, id, void *))compare
                    context:(void *)context

解説

変更可能な配列を比較関数でソートします。
比較関数は2つのエレメントを比較するために使います。最初のエレメントの方が2番目のエレメントよりも小さいときは NSOrderedAscending を返し、大きい時は NSOrderedDescending を返します。2つのエレメントが同じ場合は NSOrderedSame を返します。
第3の引数として毎回contextが渡されます、比較関数のパラメータとして使うことができます。

返り値

( void )

なし

引数

( NSInteger (*)(id, id, void *) )compare
( int (*)(id, id, void *) )compare

比較関数

( void * )context

追加情報

クラス

NSMutableArray

Instance Methods

使用可能

10.0

参照

例文

#import "MyObject.h"

@implementation MyObject

- (IBAction)myAction:(id)sender
{
NSMutableArray *ar =[[NSMutableArray alloc] initWithObjects:@"123",@"12",@"1234",@"12345",nil] ;
[ar sortUsingFunction:intSort context:NULL];

NSLog([ar description]);
}

int intSort(id val1, id val2, void *context)
{
    int iVal1 = [val1 intValue];
    int iVal2 = [val2 intValue];
    if (iVal1 < iVal2)
        return NSOrderedAscending;
    else if (iVal1 > iVal2)
        return NSOrderedDescending;
    else
        return NSOrderedSame;
}
@end