对包含匹配“对”的 NSMutableArray 进行排序价值观

发布于 2024-09-26 02:43:31 字数 670 浏览 0 评论 0原文

我不想改变 anArray 的设计方式,因为它也在其他地方用作 UITableView 的数据源。奇数数值是“用户名”,偶数数值是其匹配的“日期”。 (“对”必须保持在一起。)

我如何“按用户名”排序?

我将如何“按日期”排序?

我应该使用 sortUsingFunction 还是 sortUsingSelector?

-(void) test
{
    NSMutableArray *anArray = [NSMutableArray arrayWithObjects:@"Zeke", @"01-Jan-2010", @"Bob", @"02-Jan-2010", @"Fred", @"03-Jan-2010", @"Susan", @"04-Jan-2010", @"Kim", @"05-Jan-2010", @"Debbie", @"06-Jan-2010", nil];
    [anArray sortUsingFunction:SortByDate context:(void *)context];
}

NSComparisonResult SortByDate(NSMutableArray *a1, NSMutableArray *a2, void *context) 
{
   // What goes here to keep the pair-values "together"?
}

I'd prefer not to change the way anArray is designed because it is also used elsewhere as a dataSource for a UITableView. The odd number values are "user names", and the even number values are their matching "dates". (The "pairs" must remain together.)

How would I sort "by user name"?

How would I sort "by date"?

Should I be using sortUsingFunction or sortUsingSelector?

-(void) test
{
    NSMutableArray *anArray = [NSMutableArray arrayWithObjects:@"Zeke", @"01-Jan-2010", @"Bob", @"02-Jan-2010", @"Fred", @"03-Jan-2010", @"Susan", @"04-Jan-2010", @"Kim", @"05-Jan-2010", @"Debbie", @"06-Jan-2010", nil];
    [anArray sortUsingFunction:SortByDate context:(void *)context];
}

NSComparisonResult SortByDate(NSMutableArray *a1, NSMutableArray *a2, void *context) 
{
   // What goes here to keep the pair-values "together"?
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

眸中客 2024-10-03 02:43:31

你为什么不使用 NSDictionary?您可以将用户名作为键,将日期作为相应的值。然后,如果您想对用户名进行排序,只需使用 [dictObj allkeys] 即可获取仅包含键的数组。根据您的喜好对它们进行排序,然后在显示时显示排序后的键数组并获取键的相应值并并排显示。同样,您可以使用 [dictObj allValues] 获取所有值,对它们进行排序并将它们与键一起显示。

Why aren'e you using a NSDictionary? You can have user names as keys and the dates as corresponding values. Then if you want to sort on user names, just use [dictObj allkeys] to get an array containing just the keys. Sort them as you prefer and then when displaying display the sorted array of keys and fetch the corresponding value for the key and display along side. Similarly, you can get all the values using [dictObj allValues] sort them and display them along with keys.

┊风居住的梦幻卍 2024-10-03 02:43:31

我不想改变 anArray 的设计方式,因为它也在其他地方用作 UITableView 的数据源

这是否意味着单元格会交替显示用户名和日期,或者您是否要乘/除 2?

无论哪种方式,您都需要修复您的模型。你所说的“哎呀,排序不起作用”应该是一个很大的暗示,表明你没有选择一个好的数据表示方式。添加解决方法只会导致以后更多的眼泪。

我建议这样:

  1. 创建一个类来表示您的(用户名,日期)元组。
  2. 添加 -compareByDate: 和 -compareByUsername: 方法。
  3. [array sortUsingSelector:@selector(compareByDate:)]

如果你想在交替的单元格中显示用户名和日期,那么这仍然很容易做到,但我只是使用一个更高的单元格,这样可以省去一些痛苦。

注意: Objective-C 方法调用的开销意味着您可以通过使用 sortUsingComparator: 并定义适当的函数来获得显着的性能提升(您可以将函数粘贴在 @implmentation 和 @end 之间以让它们访问protected/private ivars),但除非您确定排序是瓶颈,否则并不真正值得这样做。

I'd prefer not to change the way anArray is designed because it is also used elsewhere as a dataSource for a UITableView

Does that mean cells alternate between displaying usernames and dates, or are you multiplying/dividing by 2?

Either way, you need to fix your model. The bit where you go "oops, sorting doesn't work" should be a big hint that you haven't chosen a good representation of your data. Adding a workaround will only lead to more tears later.

I'd suggest something like this:

  1. Create a class to represent your (username,date) tuple.
  2. Add -compareByDate: and -compareByUsername: methods.
  3. [array sortUsingSelector:@selector(compareByDate:)]

If you want to display usernames and dates in alternating cells, then it's still easy to do, but I'd simply use a taller cell and save myself some pain.

Note: The overhead of Objective-C method calls means that you can get a significant performance increase by using sortUsingComparator: and defining appropriate functions (you can stick the function between @implmentation and @end to let them access protected/private ivars) but it's not really worth doing this unless you've determined that sorting is a bottleneck.

苹果你个爱泡泡 2024-10-03 02:43:31

像这样的事情可以工作:

@class SortWrapper: NSObject
@property (copy,readwrite,atomic) NSString* name;
@property (copy,readwrite,atomic) NSDate* date;
@end

...

- (void)test
{
    NSArray* names = @[@"Alice", @"Bob", @"Cameron"];
    NSArray* dates = @[aliceDOB, bobDOB, cameronDOB]; // NSDate objects

    // turn into wrapped objects for sorting    
    NSMutableArray* wrappedObjects = [NSMutableArray array];
    for ( int i=0; i<names.count; i++ ) {
        SortWrapper* wrapped = [[SortWrapper alloc] init];
        wrapped.name = names[i];
        wrapped.date = dates[i];
        [wrappedObjects addObject:wrapped];
    }

    // to sort by date:
    NSArray* sortedByDate = [wrappedObjects sortedArrayUsingComparator:^NSComparisonResult(SortWrapper* obj1, SortWrapper* obj2) {
        return [obj1.date compare:obj2.date];
    }];

    // to sort by name:
    NSArray* sortedByName = [wrappedObjects sortedArrayUsingComparator:^NSComparisonResult(SortWrapper* obj1, SortWrapper* obj2) {
        return [obj1.name compare:obj2.name];
    }];

}   

Something like this could work:

@class SortWrapper: NSObject
@property (copy,readwrite,atomic) NSString* name;
@property (copy,readwrite,atomic) NSDate* date;
@end

...

- (void)test
{
    NSArray* names = @[@"Alice", @"Bob", @"Cameron"];
    NSArray* dates = @[aliceDOB, bobDOB, cameronDOB]; // NSDate objects

    // turn into wrapped objects for sorting    
    NSMutableArray* wrappedObjects = [NSMutableArray array];
    for ( int i=0; i<names.count; i++ ) {
        SortWrapper* wrapped = [[SortWrapper alloc] init];
        wrapped.name = names[i];
        wrapped.date = dates[i];
        [wrappedObjects addObject:wrapped];
    }

    // to sort by date:
    NSArray* sortedByDate = [wrappedObjects sortedArrayUsingComparator:^NSComparisonResult(SortWrapper* obj1, SortWrapper* obj2) {
        return [obj1.date compare:obj2.date];
    }];

    // to sort by name:
    NSArray* sortedByName = [wrappedObjects sortedArrayUsingComparator:^NSComparisonResult(SortWrapper* obj1, SortWrapper* obj2) {
        return [obj1.name compare:obj2.name];
    }];

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