iphone中对NSDate的NSString进行排序
我有一个数组,其中包含从 NSDate 转换而来的 NSString。我正在获取数组的初始索引并对它们进行排序。 日期格式为:上午 8 点,下午 1 点。当我尝试执行 NSSortDescriptor 时,它总是给我 01PM 和 08AM。
//** Date string **//
NSDate *date1 = [dateFormat dateFromString:startSection];
[dateFormat setDateFormat:@"ah"];
startSection = [dateFormat stringFromDate:date1];
NSString *strSection = [NSString stringWithFormat:@"%@", startSection];
上面的字符串被添加到 NSArray 中。下面使用 NSArray 对字符串进行排序。最后,使用相同的数组来获取首字母并在sectionforsectionIndexTitle中显示它们的索引。所以我将 AM8 和 PM1 作为部分,而不是上午 8 点和下午 1 点。
NSSortDescriptor *nameDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"start"
ascending:FALSE
selector:@selector(localizedCaseInsensitiveCompare:)] autorelease] ;
NSArray *descriptors = [NSArray arrayWithObject:nameDescriptor];
[[dict objectForKey:aKey] sortUsingDescriptors:descriptors];
I have an array which has NSString converted from NSDate. I am getting the initial index of the array and sorting them.
Dates are in format : 08AM , 01PM. When I try to do NSSortDescriptor, it always gives me 01PM and than 08AM.
//** Date string **//
NSDate *date1 = [dateFormat dateFromString:startSection];
[dateFormat setDateFormat:@"ah"];
startSection = [dateFormat stringFromDate:date1];
NSString *strSection = [NSString stringWithFormat:@"%@", startSection];
The string above is added in NSArray. The NSArray is used below to sort the strings. And finally, the same array is used to get the initial letter and display their index in sectionforsectionIndexTitle. So I have AM8 and PM1 as sections and not 8AM and 1PM.
NSSortDescriptor *nameDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"start"
ascending:FALSE
selector:@selector(localizedCaseInsensitiveCompare:)] autorelease] ;
NSArray *descriptors = [NSArray arrayWithObject:nameDescriptor];
[[dict objectForKey:aKey] sortUsingDescriptors:descriptors];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
sortUsingComparator:
怎么样?但这将涉及在比较之前将字符串从08AM
更改为AM08
并将01PM
更改为PM01
,因此假设它们将是那种形式。How about using
sortUsingComparator:
? But this will involve changing the string from08AM
toAM08
and01PM
toPM01
before comparison so this assumes that they will be of that form.