iOS NSString 字符串排序

零基础 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
)
*/

二.猜你喜欢


未经允许不得转载:猿说编程 » iOS NSString 字符串排序
喜欢(1) 打赏

评论抢沙发

评论前必须登录!

不积跬步无以至千里,不积小流无以成江海,程序人生的精彩需要坚持不懈地积累!!

开始学习

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏