iOS NSString 字符串排序

ChatGPT 3.5 国内中文镜像站免费使用啦

零基础 Object-C 学习路线推荐 : Object-C 学习目录 >> Object-C 基础

零基础 Object-C 学习路线推荐 : Object-C 学习目录 >> Object-C 线程

零基础 Object-C 学习路线推荐 : Object-C 学习目录 >> OpenGL ES

零基础 Object-C 学习路线推荐 : Object-C 学习目录 >> GPUImage

零基础 Object-C 学习路线推荐 : Object-C 学习目录 >> AVFoundation

零基础 Object-C 学习路线推荐 : Object-C 学习目录 >> CocoaPods


一.NSString 字符串的排序


1.sortedArrayUsingComparator

/******************************************************************************************/
//@Author:猿说编程
//@Blog(个人博客地址): www.codersrc.com
//@File:Object-C NSString 字符串的排序
//@Time:2021/09/02 08:00
//@Motto:不积跬步无以至千里,不积小流无以成江海,程序人生的精彩需要坚持不懈地积累!
/******************************************************************************************/

-(BOOL)stringSort
{
    NSArray *srcArray = @[@"ReplaceableVideoOrPic3",
                     @"ReplaceableText100",
                     @"ReplaceableVideoOrPic1",
                     @"ReplaceableVideoOrPic2",
                     @"ReplaceableText0",
                     @"ReplaceableVideoOrPic24",
                     @"ReplaceablePic24",
                     @"ReplaceablePic0",
                     @"ReplaceablePic5",
                     @"ReplaceableText5",
                     @"ReplaceableVideoOrPic14"];
    
    NSLog(@"srcArray:%@",srcArray);
    
    NSArray *dstArray = [srcArray sortedArrayUsingComparator:^(NSString * obj1, NSString * obj2){
     
        return (NSComparisonResult)[obj1 compare:obj2 options:NSNumericSearch];
    }];
    
    NSLog(@"dstArray:%@",dstArray);
    
    return YES;;
    
}
/*
srcArray:(
    ReplaceableVideoOrPic3,
    ReplaceableText100,
    ReplaceableVideoOrPic1,
    ReplaceableVideoOrPic2,
    ReplaceableText0,
    ReplaceableVideoOrPic24,
    ReplaceablePic24,
    ReplaceablePic0,
    ReplaceablePic5,
    ReplaceableText5,
    ReplaceableVideoOrPic14
)
dstArray:(
    ReplaceablePic0,
    ReplaceablePic5,
    ReplaceablePic24,
    ReplaceableText0,
    ReplaceableText5,
    ReplaceableText100,
    ReplaceableVideoOrPic1,
    ReplaceableVideoOrPic2,
    ReplaceableVideoOrPic3,
    ReplaceableVideoOrPic14,
    ReplaceableVideoOrPic24
)
*/

2.sortedArrayUsingSelector

/******************************************************************************************/
//@Author:猿说编程
//@Blog(个人博客地址): www.codersrc.com
//@File:Object-C NSString 字符串的排序
//@Time:2021/08/31 08:00
//@Motto:不积跬步无以至千里,不积小流无以成江海,程序人生的精彩需要坚持不懈地积累!
/******************************************************************************************/

-(BOOL)stringSort
{
    NSArray *srcArray = @[@"ReplaceableVideoOrPic3",
                     @"ReplaceableText100",
                     @"ReplaceableVideoOrPic1",
                     @"ReplaceableVideoOrPic2",
                     @"ReplaceableText0",
                     @"ReplaceableVideoOrPic24",
                     @"ReplaceablePic24",
                     @"ReplaceablePic0",
                     @"ReplaceablePic5",
                     @"ReplaceableText5",
                     @"ReplaceableVideoOrPic14"];
    
    NSLog(@"srcArray:%@",srcArray);
    
    NSArray *dstArray = [srcArray sortedArrayUsingSelector:@selector(compare:)];//这个是一直默认升序
    
    NSLog(@"dstArray:%@",dstArray);
    
    return YES;;
    
}

/*

srcArray:(
    ReplaceableVideoOrPic3,
    ReplaceableText100,
    ReplaceableVideoOrPic1,
    ReplaceableVideoOrPic2,
    ReplaceableText0,
    ReplaceableVideoOrPic24,
    ReplaceablePic24,
    ReplaceablePic0,
    ReplaceablePic5,
    ReplaceableText5,
    ReplaceableVideoOrPic14
)
dstArray:(
    ReplaceablePic0,
    ReplaceablePic24,
    ReplaceablePic5,
    ReplaceableText0,
    ReplaceableText100,
    ReplaceableText5,
    ReplaceableVideoOrPic1,
    ReplaceableVideoOrPic14,
    ReplaceableVideoOrPic2,
    ReplaceableVideoOrPic24,
    ReplaceableVideoOrPic3
)
*/

3.sortedArrayUsingDescriptors

/******************************************************************************************/
//@Author:猿说编程
//@Blog(个人博客地址): www.codersrc.com
//@File:Object-C NSString 字符串的排序
//@Time:2021/08/31 08:00
//@Motto:不积跬步无以至千里,不积小流无以成江海,程序人生的精彩需要坚持不懈地积累!
/******************************************************************************************/

-(BOOL)stringSort
{
    NSArray *srcArray = @[@"ReplaceableVideoOrPic3",
                     @"ReplaceableText100",
                     @"ReplaceableVideoOrPic1",
                     @"ReplaceableVideoOrPic2",
                     @"ReplaceableText0",
                     @"ReplaceableVideoOrPic24",
                     @"ReplaceablePic24",
                     @"ReplaceablePic0",
                     @"ReplaceablePic5",
                     @"ReplaceableText5",
                     @"ReplaceableVideoOrPic14"];
    
    NSLog(@"srcArray:%@",srcArray);
    
    //yes升序排列,no,降序排列
    NSSortDescriptor *descriptor = [NSSortDescriptor sortDescriptorWithKey:nil ascending:NO];
     
    //注意这里的ary进行排序后会生产一个新的数组指针
    NSArray *dstArray = [srcArray sortedArrayUsingDescriptors:[NSArray arrayWithObjects:descriptor, nil]];
    
    NSLog(@"dstArray:%@",dstArray);
    
    return YES;;
    
}

/*

srcArray:(
    ReplaceableVideoOrPic3,
    ReplaceableText100,
    ReplaceableVideoOrPic1,
    ReplaceableVideoOrPic2,
    ReplaceableText0,
    ReplaceableVideoOrPic24,
    ReplaceablePic24,
    ReplaceablePic0,
    ReplaceablePic5,
    ReplaceableText5,
    ReplaceableVideoOrPic14
)
dstArray:(
    ReplaceablePic0,
    ReplaceablePic24,
    ReplaceablePic5,
    ReplaceableText0,
    ReplaceableText100,
    ReplaceableText5,
    ReplaceableVideoOrPic1,
    ReplaceableVideoOrPic14,
    ReplaceableVideoOrPic2,
    ReplaceableVideoOrPic24,
    ReplaceableVideoOrPic3
)
*/

二.猜你喜欢


ChatGPT 3.5 国内中文镜像站免费使用啦
© 版权声明
THE END
喜欢就支持一下吧
点赞1 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容