如何按时间间隔而不是一天中的时间对 NSArray 进行排序?
有没有办法对比赛的完成时间进行排序?我有一个带有 NSString 字典的 NSArray,其中一个值是作为 NSString 的比赛结束时间。只要它们具有相同数量的整数,它就会正确地对它们进行排序,如下所示:54.13、54.32、54.52...但是当其中一个较长时,例如:1:13.42 - 它将它放在顶部名单。我应该如何排序,以便较慢的时间位于列表的底部。这是我目前拥有的:
NSSortDescriptor * sortByTime2 = [[NSSortDescriptor alloc] initWithKey:@"raceTime" ascending:YES];
NSArray * descriptors2 = [NSArray arrayWithObject:sortByTime2];
myArray = [myArray sortedArrayUsingDescriptors:descriptors2];
谢谢,
R
Is there a way to sort on finish times for a race? I have an NSArray with a dictionary of NSStrings and one of the values is race finish time as an NSString. It sorts them all correctly as long as they have the same number of integers, like this: 54.13, 54.32, 54.52... but when one of them is longer, for example: 1:13.42 -- It puts it at the top of the list. How should I sort this so the slower times are at the bottom of the list. Here is what I currently have:
NSSortDescriptor * sortByTime2 = [[NSSortDescriptor alloc] initWithKey:@"raceTime" ascending:YES];
NSArray * descriptors2 = [NSArray arrayWithObject:sortByTime2];
myArray = [myArray sortedArrayUsingDescriptors:descriptors2];
thanks,
R
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
字符串“raceTime”只是字符串,它们不代表时间。当你想要比较这些时,你需要将这些字符串转换为 NSData 的实例,然后排序。您可以使用 NSSortDescriptor 并使用选择器初始化它,该选择器将执行转换并返回适当的顺序。
The strings "raceTime" are just mere string, they don't represent time. When you want to compare these, you need to convert these string to instances of NSData, then sort. You can use NSSortDescriptor and initialize it with a selector which will do the conversion and return the appropriate order.