NSMutableDictionary 的value值去掉重复

来源:互联网 发布:少女前线9a91数据 编辑:程序博客网 时间:2024/06/05 09:44

遇到一个比较闷的问题。  将搜索key的记录 取出来 并且排序 +去掉重复.

上一个开发留下的 问题


首先是读取数据

    NSMutableDictionary *tempDic = [NSMutableDictionary dictionary];    NSString *sql = [NSString stringWithFormat:@"SELECT %@ FROM %@ order by id desc limit 0,12", _CACHE_VALUE, _CACHE_TABLE];    FMResultSet *set = [db executeQuery:sql];        int i = 1;    while (set.next) {        [tempDic setObject:[set objectForColumnName:_CACHE_VALUE] forKey:[NSString stringWithFormat:@"%d",i]];        i++;    }    [db close];


跟着就是去掉重复数据

NSMutableDictionary *newDict = [NSMutableDictionary dictionary];    for(id item in [tempDic allValues]){        NSArray * keys = [tempDic allKeysForObject:item];        [newDict setObject:item forKey:keys[0]];    }    NSLog(@"new dictionary is :%@",newDict);

再进行排序

NSArray* arr1 = [newDict allKeys];    arr1 = [arr1 sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2){        NSComparisonResult result = [obj1 compare:obj2];        return result == NSOrderedDescending;    }];


 最后把 记录输出一下

 for (NSString *key in arr1) {        [values addObject:[newDict objectForKey:key]];<span id="transmark"></span>    }




0 1