iphone中对NSDate的NSString进行排序

发布于 2024-11-25 16:53:11 字数 989 浏览 2 评论 0原文

我有一个数组,其中包含从 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 技术交流群。

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

发布评论

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

评论(1

总攻大人 2024-12-02 16:53:11

使用sortUsingComparator:怎么样?但这将涉及在比较之前将字符串从 08AM 更改为 AM08 并将 01PM 更改为 PM01,因此假设它们将是那种形式。

NSMutableArray * theArray = [dict objectForKey:aKey]; /* To be sorted */
[theArray sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
    NSString * string1 = [NSString stringWithFormat:@"%@%@", [(NSString *)obj1 substringFromIndex:2], [(NSString *)obj1 substringToIndex:2];
    NSString * string2 = [NSString stringWithFormat:@"%@%@", [(NSString *)obj2 substringFromIndex:2], [(NSString *)obj2 substringToIndex:2];

    return [string1 localizedCaseInsensitiveCompare:string2];
}];

/* theArray is now sorted */

How about using sortUsingComparator:? But this will involve changing the string from 08AM to AM08 and 01PM to PM01 before comparison so this assumes that they will be of that form.

NSMutableArray * theArray = [dict objectForKey:aKey]; /* To be sorted */
[theArray sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
    NSString * string1 = [NSString stringWithFormat:@"%@%@", [(NSString *)obj1 substringFromIndex:2], [(NSString *)obj1 substringToIndex:2];
    NSString * string2 = [NSString stringWithFormat:@"%@%@", [(NSString *)obj2 substringFromIndex:2], [(NSString *)obj2 substringToIndex:2];

    return [string1 localizedCaseInsensitiveCompare:string2];
}];

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