zoukankan      html  css  js  c++  java
  • 对数组中的字典进行排序

    原来字典
    NSArray *arr = @[@{@"index" : @"3", @"key" : @"value"}, @{@"index" : @"4", @"key" : @"value"}, @{@"index" : @"1", @"key" : @"value"}, @{@"index" : @"2", @"key" : @"value"}];

    要重新按照字典里的index值排序变成这样

    NSArray *arr = @[@{@"index" : @"1", @"key" : @"value"},
                     @{@"index" : @"2", @"key" : @"value"},
                     @{@"index" : @"3", @"key" : @"value"},
                     @{@"index" : @"4", @"key" : @"value"}];


    方法一:
    - (NSComparisonResult)compare:(Person *)otherObject {
        return [self.birthDate compare:otherObject.birthDate];
    }
    NSArray *sortedArray;
    sortedArray = [drinkDetails sortedArrayUsingSelector:@selector(compare:)];

    方法二:(推荐)
    NSSortDescriptor *sortDescriptor;
    sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"birthDate"
                                                  ascending:YES] autorelease];
    NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
    NSArray *sortedArray;
    sortedArray = [drinkDetails sortedArrayUsingDescriptors:sortDescriptors];


    方法三:
    NSArray *sortedArray;
    sortedArray = [drinkDetails sortedArrayUsingComparator:^NSComparisonResult(id a, id b) {
        NSDate *first = [(Person*)a birthDate];
        NSDate *second = [(Person*)b birthDate];
        return [first compare:second];
    }];
     
     
  • 相关阅读:
    ec20 queclocator V1. 0 test
    javascript JSON.parse and JSON.stringify
    linux command pushd popd
    learning gcc #pragma once
    learning gcc __BEGIN_DECLS and __END_DECLS
    aarch-linux-gnu-g++ install
    启用”开始“菜单中的“运行”功能
    获取本机安装的软件清单
    固定任务栏
    优化菜单显示速度
  • 原文地址:https://www.cnblogs.com/Xujg/p/5325080.html
Copyright © 2011-2022 走看看