Objective-C,对多维数组进行排序
我在 Objective-C 中对多维数组进行排序时遇到了一些麻烦。我基本上有一个数组,其中每个元素都是以下形式的数组:
(NSString, NSDate, NSString, NSString)
这样我的顶级数组具有以下形式:
(
(NSString, NSDate, NSString, NSString),
(NSString, NSDate, NSString, NSString),
(NSString, NSDate, NSString, NSString),
(NSString, NSDate, NSString, NSString),
...
)
我希望能够根据顶级数组的元素自己的任何元素对它们进行排序。我编写了以下代码来执行此操作,但事实证明对于我正在处理的大型数据集来说效率太低。
-(NSMutableArray *) sortArrayByDate:(NSMutableArray *) unsortedArray {
NSMutableArray * sortedArray = [[NSMutableArray alloc ] init ];
while ([unsortedArray count]>0) {
int topIndex = 0;
NSDate * topDate = [[NSDate alloc] initWithString:@"1970-01-01 00:00:00 +0600"];
for(int j=0;j<[unsortedArray count];j++) {
NSDate * targetDate = [[unsortedArray objectAtIndex:j] objectAtIndex:1];
if ([targetDate compare:topDate] == NSOrderedDescending) {
topDate = targetDate;
topIndex = j;
}
}
[sortedArray addObject:[unsortedArray objectAtIndex:topIndex]];
[unsortedArray removeObjectAtIndex:topIndex];
}
return sortedArray;
}
任何人都可以建议如何使用 sortUsingSelector 或 sortUsingDescriptor 等更成熟的方法来完成此任务吗?如果我对一维数组进行排序,我认为它会是这样的:
[unsortedArray sortUsingSelector: @selector(compare:)]
但是我如何告诉它使用我传递的数组的第 n 个值进行排序?
-非常感谢
-非常感谢
I've been having some trouble with sorting multi-dimensional arrays in Objective-C. I basically have an array of which every element is an array of the form:
(NSString, NSDate, NSString, NSString)
such that my top level array has the form:
(
(NSString, NSDate, NSString, NSString),
(NSString, NSDate, NSString, NSString),
(NSString, NSDate, NSString, NSString),
(NSString, NSDate, NSString, NSString),
...
)
I would like to be able to sort the elements of the top level array based on any of of their own elements. I wrote the following code which does this, but has proven to be far too inefficient for the large data sets I am dealing with.
-(NSMutableArray *) sortArrayByDate:(NSMutableArray *) unsortedArray {
NSMutableArray * sortedArray = [[NSMutableArray alloc ] init ];
while ([unsortedArray count]>0) {
int topIndex = 0;
NSDate * topDate = [[NSDate alloc] initWithString:@"1970-01-01 00:00:00 +0600"];
for(int j=0;j<[unsortedArray count];j++) {
NSDate * targetDate = [[unsortedArray objectAtIndex:j] objectAtIndex:1];
if ([targetDate compare:topDate] == NSOrderedDescending) {
topDate = targetDate;
topIndex = j;
}
}
[sortedArray addObject:[unsortedArray objectAtIndex:topIndex]];
[unsortedArray removeObjectAtIndex:topIndex];
}
return sortedArray;
}
Can anyone please make a suggestion on how to accomplish this task using the more established methods of either sortUsingSelector or sortUsingDescriptor? If I was sorting a 1D array I think it would be something like:
[unsortedArray sortUsingSelector: @selector(compare:)]
but how do I tell it to sort using the nth value of the array I am passing it?
-Many thanks
-Many Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在大多数情况下,您会创建一个对象:
然后教它将自己与其他对象进行比较,然后使用数组中的这些对象。数据和实施的逻辑组织。
in most cases, you would create an object:
then teach it to compare itself to others, then use those objects in the array. logical organization of data and implementation.
您还可以使用基于块的方法
sortUsingComparator:
中NSMutableArray
像这样 -你还有一个并行方法
sortedArrayUsingComparator:
inNSArray
它将输出一个排序数组。按不同索引排序
根据传递的索引,数组将根据该索引处的对象进行排序。这是非常基本的代码,但您可以添加它。
You can also use blocks-based method
sortUsingComparator:
inNSMutableArray
like this –You also have a parallel method
sortedArrayUsingComparator:
inNSArray
which will spew out a sorted array.Sorting by varying indices
Depending on the index passed, the array will sort based on objects at that index. This is pretty basic code but you can add to this.