基金项目中遇到的两种排序

这次做的关于基金的项目里有比较多的排序需求,这里简单的把我遇到的几种情况总结一下,方便以后查阅。

升序降序排列数据

升序降序主要是利用NSArray的sortedArrayUsingComparator来实现,通过循环数组中的元素来对数组进行重新排序

- (NSArray *)sortedArrayUsingComparator:(NSComparator NS_NOESCAPE)cmptr API_AVAILABLE(macos(10.6), ios(4.0), watchos(2.0), tvos(9.0));

例如给 1,3,5,2,6,7,8,3,5,7,6,7,1,2,9 排序,分别使用得到他们的升序和降续,例子如下:

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    NSArray *array = @[@"1",@"3",@"5",@"2",@"6",@"7",@"8",@"3",@"5",@"7",@"6",@"7",@"1",@"2",@"9"];
    [self arraySortASCWithArray:array];
    [self arraySortDESCWithArray:array];
}
#pragma mark -- 数组排序方法(升序:从小到大)
- (NSArray *)arraySortASCWithArray:(NSArray *)array {
    NSArray *result = [array sortedArrayUsingComparator:^NSComparisonResult(NSString *obj1, NSString *obj2) {
        return [@(obj1.floatValue) compare:@(obj2.floatValue)];
    }];
    return result;
}
#pragma mark -- 数组排序方法(降序:从大到小)
- (NSArray *)arraySortDESCWithArray:(NSArray *)array {
    NSArray *result = [array sortedArrayUsingComparator:^NSComparisonResult(NSString *obj1, NSString *obj2) {
        return [@(obj2.floatValue) compare:@(obj1.floatValue)];
    }];
    return result;
}

根据已有的排列顺序对所得数组进行排序

这个需求是在已有一个默认排序的前提下给一个不确定的数组进行排序,例如对基金类型进行排序,基金的类型常见的有混合型,股票型,指数型,债券型,货币型,封闭型。已知这个默认的顺序,然后对服务端给的数据进行排序,例子如下:

- (void)viewDidLoad {
    [super viewDidLoad];
    NSDictionary *dic1 = @{@"指数型":@"基金1"};
    NSDictionary *dic2 = @{@"混合型":@"基金2"};
    NSDictionary *dic3 = @{@"混合型":@"基金3"};
    NSDictionary *dic4 = @{@"封闭型":@"基金4"};
    NSDictionary *dic5 = @{@"股票型":@"基金5"};
    NSDictionary *dic6 = @{@"AA型":@"基金6"};
    NSDictionary *dic7 = @{@"货币型":@"基金7"};
    NSDictionary *dic8 = @{@"货币型":@"基金8"};
    NSDictionary *dic9 = @{@"指数型":@"基金9"};
    NSDictionary *dic10 = @{@"BB型":@"基金10"};
    NSDictionary *dic11 = @{@"货币型":@"基金11"};
    NSDictionary *dic12 = @{@"股票型型":@"基金12"};
    NSArray *fundArray = @[dic1,dic3,dic2,dic11,dic12,dic3,dic4,dic5,dic6,dic7,dic7,dic9,dic5,dic8,dic11,dic10,dic2];
    NSLog(@"_________%@",[self getFundTypeArrayWithDataArray:fundArray]);
}

- (NSMutableArray *)getFundTypeArrayWithDataArray:(NSArray *)dataArray {
    NSMutableArray *filterArray = [NSMutableArray array];

    NSMutableDictionary *allArrayDic = [NSMutableDictionary new];
    for (NSDictionary *dic in dataArray) {
        NSString *type = dic.allKeys[0];
        if ([allArrayDic.allKeys containsObject:type]) {
            NSMutableArray *array = allArrayDic[type];
            [array addObject:dic];
            [allArrayDic setObject:array forKey:type];
        } else {
            NSMutableArray *array = [NSMutableArray new];
            [array addObject:dic];
            [allArrayDic setObject:array forKey:type];
        }
    }
    
    NSArray *defaultArray = @[@"混合型",@"股票型",@"指数型",@"债券型",@"货币型",@"封闭型"];
    
    for (NSString *fundTypeStr in defaultArray) {
        if ([allArrayDic.allKeys containsObject:fundTypeStr])  {
            NSArray *fundArray = allArrayDic[fundTypeStr];
            [filterArray addObject:fundArray];
            [allArrayDic removeObjectForKey:fundTypeStr];
        }
    }
    // 把剩下非排列基金的直接放进去
    for (NSString *keystr in allArrayDic.allKeys) {
        NSArray *fundArray = allArrayDic[keystr];
        [filterArray addObject:fundArray];
    }
    
    return filterArray;
}


正常情况下都是用model来代替这个dic,不过篇幅限制,就直接用字典来表示了

你可能感兴趣的:(基金项目中遇到的两种排序)