您的位置:首页 > 移动开发 > IOS开发

iOS oc可变数组排序方法

2015-12-18 21:01 447 查看
1.首先定义可变数组

@property (nonatomic,strong)NSMutableArray * loadAllStylist;

-(NSMutableArray *)loadAllStylist{

    if (!_loadAllStylist) {

        _loadAllStylist = [NSMutableArrayarray];

    }

    return_loadAllStylist;

}

2.数据从数据库加载已转BloggerInfo模型类型

 NSArray* stylists = [ModeDatabasereadDataFromTableName:BLOGGER_INFO_TABLE_NAMEwithConditionDictionary:nilcallbackClass:[NSArrayclass]];

    if ([stylists
diffClassArray]) {

        [self.loadAllStylistaddObjectsFromArray:stylists];

3.(重点排序)可变数组调用这个方法排序(默认升序):

[self.loadAllStylistsortUsingComparator:^NSComparisonResult(id _Nonnull
obj1, id _Nonnull obj2) {

            BloggerInfo* b1 = obj1;

            BloggerInfo* b2 = obj2;

            return [b1.nicknamecompare:b2.nickname options:NSCaseInsensitiveSearch];//大小写不敏感的搜索

        }];

4.一些可选参数
options:

typedef NS_OPTIONS(NSUInteger, NSStringCompareOptions) {

    NSCaseInsensitiveSearch = 1,         //大小写不敏感的搜索

    NSLiteralSearch = 2,/* Exact character-by-character equivalence */

    NSBackwardsSearch = 4,/* Search from end of source string */

    NSAnchoredSearch = 8,/* Search is limited to start (or end, if NSBackwardsSearch) of source string */

    NSNumericSearch = 64,/* Added in 10.2; Numbers within strings are compared using numeric value, that is, Foo2.txt
< Foo7.txt < Foo25.txt; only applies to compare methods, not find */

    NSDiacriticInsensitiveSearch NS_ENUM_AVAILABLE(10_5,2_0)
= 128,
/* If specified, ignores diacritics (o-umlaut == o) */

    NSWidthInsensitiveSearch NS_ENUM_AVAILABLE(10_5,2_0)
= 256,
/* If specified, ignores width differences ('a' == UFF41) */

    NSForcedOrderingSearch NS_ENUM_AVAILABLE(10_5,2_0)
= 512,
/* If specified, comparisons are forced to return either NSOrderedAscending or NSOrderedDescending if the strings are equivalent but not strictly equal, for stability when sorting (e.g. "aaa" > "AAA" with NSCaseInsensitiveSearch specified) */

    NSRegularExpressionSearch NS_ENUM_AVAILABLE(10_7,3_2)
= 1024   
/* Applies to rangeOfString:..., stringByReplacingOccurrencesOfString:..., and replaceOccurrencesOfString:... methods only; the search string is treated as an ICU-compatible regular expression; if set, no other options can apply except NSCaseInsensitiveSearch
and NSAnchoredSearch */

};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: