美文网首页
iOS sortedArrayUsingComparator数组

iOS sortedArrayUsingComparator数组

作者: Singularity_Lee | 来源:发表于2020-05-22 16:27 被阅读0次

数组的正序排序倒序排序降序排列升序排列都是常用到的。而系统为了方便使用简化代码而提供有sortedArrayUsingComparator数组排序方法

数组倒序

    NSArray *originArray=@[@2,@5,@4,@1,@3,@7,@6];
    NSArray *resultArray=[originArray sortedArrayUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
        
        return NSOrderedDescending;
    }];
    DLog(@"%@>>>数组倒序",resultArray);

("6","7","3","1","4","5","2",)>>>数组倒序

数组正序

    NSArray *originArray=@[@2,@5,@4,@1,@3,@7,@6];
    NSArray *resultArray=[originArray sortedArrayUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
        
        return NSOrderedAscending;
    }];
    DLog(@"%@>>>数组正序",resultArray);

("2","5","4","1","3","7","6",)>>>数组正序

数组降序

    NSArray *originArray=@[@2,@5,@4,@1,@3,@7,@6];
    NSArray *resultArray=[originArray sortedArrayUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
        if ([obj1 integerValue]>[obj2 integerValue]) {
            return NSOrderedAscending;
        }
        return NSOrderedDescending;
    }];
    DLog(@"%@>>>数组降序",resultArray);

("7","6","5","4","3","2","1",)>>>数组降序

数组升序

    NSArray *originArray=@[@2,@5,@4,@1,@3,@7,@6];
    NSArray *resultArray=[originArray sortedArrayUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
        if ([obj1 integerValue]>[obj2 integerValue]) {
            return NSOrderedDescending;
        }
        return NSOrderedAscending;
//        return [[NSNumber numberWithInteger:[obj1 integerValue]] compare:[NSNumber numberWithInteger:[obj2 integerValue]]];
    }];
    DLog(@"%@>>>数组升序",resultArray);

("1","2","3","4","5","6","7",)>>>数组升序


排序比较结果
/*
 These constants are used to indicate how items in a request are ordered, from the first one given in a method invocation or function call to the last (that is, left to right in code).
 
 Given the function:
   NSComparisonResult f(int a, int b)
 
 If:
    a < b   then return NSOrderedAscending. The left operand is smaller than the right operand.
    a > b   then return NSOrderedDescending. The left operand is greater than the right operand.
    a == b  then return NSOrderedSame. The operands are equal.
*/
typedef NS_CLOSED_ENUM(NSInteger, NSComparisonResult) {
    NSOrderedAscending = -1L,
    NSOrderedSame,
    NSOrderedDescending
};
NSComparisonResult
NSOrderedAscending 升序 (左<右)
NSOrderedSame 相等 (左=右)
NSOrderedDescending 降序 (左>右)

NSStringCompareOptions
/* These options apply to the various search/find and comparison methods (except where noted).
*/
typedef NS_OPTIONS(NSUInteger, NSStringCompareOptions) {
    NSCaseInsensitiveSearch        /*不区分大小写比较*/
    NSLiteralSearch                /*区分大小写比较*/
    NSBackwardsSearch              /*从字符串末尾开始搜索*/
    NSAnchoredSearch               /*搜索限制范围的字符串*/
    NSNumericSearch                /*按照字符串里的数字为依据,算出顺序*/
    NSDiacriticInsensitiveSearch   /*忽略 "-" 符号的比较*/ 
    NSWidthInsensitiveSearch       /*忽略字符串的长度,比较出结果*/
    NSForcedOrderingSearch         /*忽略不区分大小写比较的选项,并强制返回NSOrderedAscending 或者 NSOrderedDescending*/
    NSRegularExpressionSearch      /*只能应用于 rangeOfString:..., stringByReplacingOccurrencesOfString:...和 replaceOccurrencesOfString:... 方法。使用通用兼容的比较方法,如果设置此项,可以去掉 NSCaseInsensitiveSearch 和 NSAnchoredSearch*/
};

相关文章

网友评论

      本文标题:iOS sortedArrayUsingComparator数组

      本文链接:https://www.haomeiwen.com/subject/pmgqahtx.html