Objective-C,对多维数组进行排序

发布于 2024-11-08 21:54:51 字数 1482 浏览 0 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

昇り龍 2024-11-15 21:54:51

在大多数情况下,您会创建一个对象:

@interface MONObject : NSObject
{
  NSString * a;
  NSDate * b;
  NSString * c;
  NSString * d;
}
...

然后教它将自己与其他对象进行比较,然后使用数组中的这些对象。数据和实施的逻辑组织。

in most cases, you would create an object:

@interface MONObject : NSObject
{
  NSString * a;
  NSDate * b;
  NSString * c;
  NSString * d;
}
...

then teach it to compare itself to others, then use those objects in the array. logical organization of data and implementation.

和我恋爱吧 2024-11-15 21:54:51

您还可以使用基于块的方法 sortUsingComparator:NSMutableArray 像这样 -

[myArray sortUsingComparator:^(id first, id second){
    id firstObject = [first objectAtIndex:1];
    id secondObject = [second objectAtIndex:1];

    return [firstObject compare:secondObject];
}]

你还有一个并行方法 sortedArrayUsingComparator: in NSArray 它将输出一个排序数组。

按不同索引排序

typedef NSComparator (^ComparatorFactory)(id);

ComparatorFactory comparatorForIndex = ^NSComparator(id context) {
    NSInteger index = [(NSNumber*)context integerValue];
    NSComparator comparator = ^(id first, id second) {
        id firstObject = [first objectAtIndex:index];
        id secondObject = [second objectAtIndex:index];

        return [firstObject compare:secondObject];
    };

    return [[comparator copy] autorelease];
};

[myArray sortUsingComparator:comparatorForIndex([NSNumber numberWithInteger:1])];

根据传递的索引,数组将根据该索引处的对象进行排序。这是非常基本的代码,但您可以添加它。

You can also use blocks-based method sortUsingComparator: in NSMutableArray like this –

[myArray sortUsingComparator:^(id first, id second){
    id firstObject = [first objectAtIndex:1];
    id secondObject = [second objectAtIndex:1];

    return [firstObject compare:secondObject];
}]

You also have a parallel method sortedArrayUsingComparator: in NSArray which will spew out a sorted array.

Sorting by varying indices

typedef NSComparator (^ComparatorFactory)(id);

ComparatorFactory comparatorForIndex = ^NSComparator(id context) {
    NSInteger index = [(NSNumber*)context integerValue];
    NSComparator comparator = ^(id first, id second) {
        id firstObject = [first objectAtIndex:index];
        id secondObject = [second objectAtIndex:index];

        return [firstObject compare:secondObject];
    };

    return [[comparator copy] autorelease];
};

[myArray sortUsingComparator:comparatorForIndex([NSNumber numberWithInteger:1])];

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文