NSSortDescriptor数组排序

作者: 追沐 | 来源:发表于2017-09-04 17:43 被阅读5次

    NSSortDescriptor可以实现对数组的排序,经常可用于按照首字母进行排序等。

    按照升序或者降序,比如@"e",@"a",@"c",@"b"按照生序顺序为@"a",@"b",@"c",@"e"。

    将@"3",@"1",@"4",@"0",@"2",@"10",@"22"按照升序列为:@"0",@"1",@"10",@"2",@"22",@"3",@"4"。

    用到的方法

    - (NSArray<ObjectType> *)sortedArrayUsingDescriptors:(NSArray<NSSortDescriptor *> *)sortDescriptors;    // returns a new array by sorting the objects of the receiver
    
    - (NSArray<ObjectType> *)sortedArrayUsingDescriptors:(NSArray<NSSortDescriptor *> *)sortDescriptors;    // returns a new array by sorting the objects of the receiver
    

    ascending为升序或者降序,YES为升序。

    按照首数字顺序排序

    NSMutableArray *datasource = [[NSMutableArray alloc] init];
    [datasource addObjectsFromArray:@[@"3",@"1",@"4",@"0",@"2",@"10",@"22"]];
    
    NSSortDescriptor *descriptor = [NSSortDescriptor sortDescriptorWithKey:@"self" ascending:YES];
    
    NSArray *newArray = [datasource sortedArrayUsingDescriptors:[NSArray arrayWithObjects:descriptor, nil]];
    
    for (NSString *distance in newArray) {
        NSLog(@"%@", distance);
    }
    

    打印结果:

    2017-09-04 17:07:09.768 Test——1[93764:646098] 0
    2017-09-04 17:07:09.768 Test——1[93764:646098] 1
    2017-09-04 17:07:09.768 Test——1[93764:646098] 10
    2017-09-04 17:07:09.768 Test——1[93764:646098] 2
    2017-09-04 17:07:09.768 Test——1[93764:646098] 22
    2017-09-04 17:07:09.769 Test——1[93764:646098] 3
    2017-09-04 17:07:09.769 Test——1[93764:646098] 4
    

    按照首字母排序

    NSMutableArray *datasource = [[NSMutableArray alloc] init];
    [datasource addObjectsFromArray:@[@"meng",@"xaing",@"bian",@"jian",@"bo",@"wang",@"wan"]];
    
    NSSortDescriptor *descriptor = [NSSortDescriptor sortDescriptorWithKey:@"self" ascending:YES];
    
    NSArray *newArray = [datasource sortedArrayUsingDescriptors:[NSArray arrayWithObjects:descriptor, nil]];
    
    for (NSString *distance in newArray) {
        NSLog(@"%@", distance);
    }
    

    结果:

    2017-09-04 17:34:56.268 Test——1[93969:655792] bian
    2017-09-04 17:34:56.268 Test——1[93969:655792] bo
    2017-09-04 17:34:56.268 Test——1[93969:655792] jian
    2017-09-04 17:34:56.268 Test——1[93969:655792] meng
    2017-09-04 17:34:56.269 Test——1[93969:655792] wan
    2017-09-04 17:34:56.269 Test——1[93969:655792] wang
    2017-09-04 17:34:56.269 Test——1[93969:655792] xaing
    

    按照key排序

    NSMutableArray *datasource = [[NSMutableArray alloc] init];
    
    NSMutableDictionary *dic1 = [[NSMutableDictionary alloc] init];
    [dic1 setObject:@"zhangfei" forKey:@"name"];
    
    NSMutableDictionary *dic2 = [[NSMutableDictionary alloc] init];
    [dic2 setObject:@"libai" forKey:@"name"];
    
    NSMutableDictionary *dic3 = [[NSMutableDictionary alloc] init];
    [dic3 setObject:@"dufu" forKey:@"name"];
    
    [datasource addObject:dic1];
    [datasource addObject:dic2];
    [datasource addObject:dic3];
    
    NSSortDescriptor *descriptor = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES];
    
    NSArray *array = [datasource sortedArrayUsingDescriptors:[NSArray arrayWithObjects:descriptor, nil]];
    
    for (NSMutableDictionary *md in array) {
        NSLog(@"%@", [md objectForKey:@"name"]);
    }
    

    打印结果

    2017-09-04 17:43:05.833 Test——1[94084:659345] dufu
    2017-09-04 17:43:05.833 Test——1[94084:659345] libai
    2017-09-04 17:43:05.833 Test——1[94084:659345] zhangfei
    

    相关文章

      网友评论

        本文标题:NSSortDescriptor数组排序

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