【发布时间】:2014-03-01 07:24:32
【问题描述】:
我有一个由两个文本字段组成的对象类的 NSArray。我想按升序对这些对象进行排序,之前我已经使用 NSDictionary 对象完成了此操作,但是我现在已经更改为我创建的对象类,所以我真的不知道如何比较值以获取排序后的数组。
对象变量是NSNumbers,但只包含数字值,我认为这会影响事情。
这就是我使用旧代码对 NSDictionary 值进行排序的方式。
NSArray *tempSortedItemsArray = [installArray sortedArrayUsingDescriptors:
@[[NSSortDescriptor
sortDescriptorWithKey:@"first" ascending:YES],
[NSSortDescriptor
sortDescriptorWithKey:@"second" ascending:YES]]];
sortedItemsArray = [tempSortedItemsArray mutableCopy];
tempSortedItemsArray = nil;
所以如果我有一个这样的对象数组
(第一/第二)
2 1
0 0
1 0
1 1
2 2
2 0
3 0
如果会这样排序
0 0
1 0
1 1
2 0
2 1
2 2
3 0
对于使用 NSNumber 变量 first 和 second 调整 NSObject 类的任何帮助将不胜感激。
【问题讨论】:
-
为什么要对包含文本字段的对象数组进行排序?为什么自定义对象不只包含数据?我认为你在做一些奇怪的事情。
标签: ios nsarray nsdictionary nsnumber nsobject