ios 数组中的字典排序_iOS将数组和字典,倒叙或顺序排序

1.倒序

在ios开发的过程中,经常需要使数组中的数据倒叙排列!比如将返回得到的数据,从大到小重新排列顺序

NSMutableArray *temp = [NSMutableArray

arrayWithObjects:@"5",@"1",@"4",@"2",nil];

temp = (NSMutableArray *)[[temp

reverseObjectEnumerator] allObjects];

2.升序/

降序

字符串

sortedArrayUsingSelector

//通过自带的compare方法升序排列

NSMutableArray *array =

[[NSMutableArray alloc]

initWithObjects:@"5",@"1",@"4",@"2",nil];

//通过倒序的方法进行降序排列

NSEnumerator *enumerator =

[array

reverseObjectEnumerator];

array =[[NSMutableArray

alloc]initWithArray: [enumerator

allObjects]];

字典

按Key值大小对NSDictionary排序

sortedArrayUsingSelector

NSMutableArray *array = [NSMutableArray

arrayWithObjects:

[NSDictionary dictionaryWithObjectsAndKeys:@"Obj0", [NSNumber

numberWithInt:0], nil],

[NSDictionary dictionaryWithObjectsAndKeys:@"Obj5", [NSNumber

numberWithInt:5], nil],

[NSDictionary dictionaryWithObjectsAndKeys:@"Obj2", [NSNumber

numberWithInt:2], nil],

[NSDictionary dictionaryWithObjectsAndKeys:@"Obj3", [NSNumber

numberWithInt:3], nil],

[NSDictionary

dictionaryWithObjectsAndKeys:@"Obj1", [NSNumber numberWithInt:1],

nil],

[NSDictionary dictionaryWithObjectsAndKeys:@"Obj4", [NSNumber

numberWithInt:4], nil], nil];

NSArray *resultArray = [array

sortedArrayUsingSelector:@selector(compare:)];

//因为NSDictionary没有compare的排序比较方法,所以需要我们自己写一个

- (NSComparisonResult)compare: (NSDictionary

*)otherDictionary{

NSDictionary *tempDictionary = (NSDictionary

*)self;

NSNumber *number1 = [[tempDictionary allKeys]

objectAtIndex:0];

NSNumber *number2 = [[otherDictionary allKeys]

objectAtIndex:0];

NSComparisonResult result = [number1

compare:number2];

return

result == NSOrderedDescending; // 升序

// return result == NSOrderedAscending; //

降序

}

sortedArrayUsingComparator

NSMutableArray *array = [NSMutableArray

arrayWithObjects:

[NSDictionary dictionaryWithObjectsAndKeys:@"Obj0", [NSNumber

numberWithInt:0], nil],

[NSDictionary dictionaryWithObjectsAndKeys:@"Obj5", [NSNumber

numberWithInt:5], nil],

[NSDictionary dictionaryWithObjectsAndKeys:@"Obj2", [NSNumber

numberWithInt:2], nil],

[NSDictionary dictionaryWithObjectsAndKeys:@"Obj3", [NSNumber

numberWithInt:3], nil],

[NSDictionary dictionaryWithObjectsAndKeys:@"Obj1", [NSNumber

numberWithInt:1], nil],

[NSDictionary dictionaryWithObjectsAndKeys:@"Obj4", [NSNumber

numberWithInt:4], nil], nil];

// NSArray *resultArray = [array

sortedArrayUsingSelector:@selector(compare:)];

NSArray *resultArray = [array

sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2)

{

NSNumber *number1 = [[obj1 allKeys] objectAtIndex:0];

NSNumber *number2 = [[obj2 allKeys] objectAtIndex:0];

NSComparisonResult result = [number1 compare:number2];

return result == NSOrderedDescending; // 升序

// return result == NSOrderedAscending; //

降序

}];

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值