不可变数组或者可变数组进行排序

#import <Foundation/Foundation.h>

NSInteger myCompare(id obj1,id obj2,void *context)

{

    //不分大小写进行升序排序

    //return [obj1 caseInsensitiveCompare:obj2];

   return -[obj1 caseInsensitiveCompare:obj2];

}

int main(int argc, const char * argv[])

{

 

    @autoreleasepool {

        

        

        NSArray *array=@[@"one", @"Two", @"Three", @"Four"];

        NSMutableArray *mutableArray=[NSMutableArrayarrayWithArray:array];

        //1不可变数组通过自定义比较规则来比较字符串

       NSArray *result= [array sortedArrayUsingFunction:myComparecontext:nil];

        NSLog(@"%@\n",result);

        //类提供compare方法不想改变数组顺序

        [array sortedArrayUsingSelector:@selector(compare:)];

        NSLog(@"%@\n",array);

        

        //2直接在数组内排序

        [mutableArray sortUsingFunction:myCompare context:nil];

        NSLog(@"%@\n",mutableArray);

 

        

    }

    return 0;

}

 

posted @ 2014-05-25 16:32  曹县三胖暴打大猩猩  阅读(301)  评论(0编辑  收藏  举报