开发者

String sorting problem NSFetchedResultsController

I have set a string (which is in fact a string) as the sectionNameKeyPath. It's sorts fine, but for example 16 september 2011, is in an earlier section than 2 september 2011. What is the best way to work around this problem

String sorting problem NSFetchedResultsController

Update My workaround is instead of a string, make an int from it (e.g. 20110909 for 201开发者_开发知识库1-09-09) and then later in titleForHeader method turn it in a string again


This is because as in any other language, this is sorted as an String and the first character 3 is bigger than 1. You will have to implement your own comparator if you want them to sort any other way.


I advise to convert NSString back to NSDate and use standard [NSDate compare:] method.


Try converting your NSStrings to NSDates and then sort them, something like this:

NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setDateFormat:@"d MMM yyyy"];
NSDate *date = [formatter dateFromString:@"3 September 2011"];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜