NSDictionary*params=@{@"v":@"1.2",@"suk":@"vod",@"type":@"2",@"from":@"ios",@"aoe":@"tt01",@"tyot":@"top5"};
NSArray*keyArray=[params allKeys];
//将键进行排序
NSArray *sortArray = [keyArray sortedArrayUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) {
return [obj1 compare:obj2 options:NSNumericSearch];
}];
NSLog(@"sortKeyArr->%@",sortArray);
//根据键获取对应的值进行排序
NSMutableArray *valueArray = [NSMutableArray array];
for (NSString *sortString in sortArray) {
[valueArray addObject:[params objectForKey:sortString]];
}
NSLog(@"sortValueArr->%@",valueArray);
//键值对拼接成数组
NSMutableArray *signArray = [NSMutableArray array];
for (int i = 0; i < sortArray.count; i++) {
NSString *keyValueStr = [NSString stringWithFormat:@"%@:%@",sortArray[i],valueArray[i]];
[signArray addObject:keyValueStr];
}
NSLog(@"signArray->%@",signArray);
NSString*paramStr2=@"aoe=tt01&from=ios&suk=vod&tyot=top5&type=2&v=1.2";
//将数组拆分成字符串进行&拼接
NSString *sign = [signArray componentsJoinedByString:@"&"];
NSLog(@"%@",sign);
//将数组中的':'替换成'='号
NSString*replaceArr=[sign stringByReplacingOccurrencesOfString:@":" withString:@"="];
NSLog(@"%@",replaceArr);
NSArray *array = [NSArray arrayWithObjects:@"z",@"2", @"3", @"1", @"4",@"b",@"a",@"c", nil];
// 返回一个排好序的数组,原来数组的元素顺序不会改变
// 指定元素的比较方法:compare:
NSArray *array2 = [array sortedArrayUsingSelector:@selector(compare:)];
NSLog(@"array2:%@", array2);
NSMutableArray *p = [[NSMutableArray alloc] initWithObjects:@"S1",@"S14",@"S14",@"S4",@"S12",@"S6",@"S7",@"S8",@"S9",@"S10",@"S11",@"S5",@"S13",@"S2",nil];
[p sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
NSString *a = (NSString *)obj1;
NSString *b = (NSString *)obj2;
int aNum = [[a substringFromIndex:1] intValue];
int bNum = [[b substringFromIndex:1] intValue];
if (aNum > bNum) {
return NSOrderedDescending;
}
else if (aNum < bNum){
return NSOrderedAscending;
}
else {
return NSOrderedSame;
}
}];
NSLog(@"p %@", p);