对包含匹配“对”的 NSMutableArray 进行排序价值观
我不想改变 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你为什么不使用 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.
这是否意味着单元格会交替显示用户名和日期,或者您是否要乘/除 2?
无论哪种方式,您都需要修复您的模型。你所说的“哎呀,排序不起作用”应该是一个很大的暗示,表明你没有选择一个好的数据表示方式。添加解决方法只会导致以后更多的眼泪。
我建议这样:
如果你想在交替的单元格中显示用户名和日期,那么这仍然很容易做到,但我只是使用一个更高的单元格,这样可以省去一些痛苦。
注意: Objective-C 方法调用的开销意味着您可以通过使用 sortUsingComparator: 并定义适当的函数来获得显着的性能提升(您可以将函数粘贴在 @implmentation 和 @end 之间以让它们访问protected/private ivars),但除非您确定排序是瓶颈,否则并不真正值得这样做。
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:
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.
像这样的事情可以工作:
Something like this could work: