本文介绍了sortedArrayUsingComparator int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个字典数组,它对sortedArrayUsingComparator对其进行排序;
I have an array of a dictionary that it sortedArrayUsingComparator sorts them based on the key;
listPerc = [listItem sortedArrayUsingComparator: ^(id a, id b) {
NSString *first = [a objectForKey:@"perc"];
NSString *second = [b objectForKey:@"perc"];
return [first compare:second];
问题在于此键是一个数字,而顺序例如是:1,10、15、17、2、23等.它不计算数字的大小.我该怎么办?
the problem is that this key is a numbers, and order is so for example: 1,10, 15, 17, 2, 23, etc. it does not calculate the magnitude of the number. how can I do?
推荐答案
将它们放入NSNumber对象中,然后进行比较.可以将它们存储为以NSNumbers开头(首选方法),也可以在比较时进行转换(更慢).
Make them into NSNumber objects and compare those instead. Either store them as NSNumbers to start with (preferred method) or convert them when comparing (slower).
将NSString转换为NSNumber可以通过以下方式完成:
Converting an NSString to an NSNumber can be done with:
NSNumberFormatter * f = [[NSNumberFormatter alloc] init];
[f setNumberStyle:NSNumberFormatterDecimalStyle];
NSNumber * myNumber = [f numberFromString:string];
[f release];
这篇关于sortedArrayUsingComparator int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!