开发者

String sorting problem NSFetchedResultsController

开发者 https://www.devze.com 2023-04-01 11:06 出处:网络
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

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

精彩评论

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