我在排序一个数组w.r.t数据库时遇到了一个问题:
NSSortDescriptor *sorter = [[NSSortDescriptor alloc] initWithKey:@"w" ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObject: sorter];
[mGlossaryArray sortUsingDescriptors:sortDescriptors];
[sorter release];
在这里的数据库中,有一些第一个大写字母,由于大写字母,它没有向我显示正确的排序输出。在这里,我使用r.t "w“对一个数组进行排序,这是我在数据库中的表列。这里我附上了输出的屏幕截图,显示“癌症”排在"c“之前,但这是不正确的,因为它没有按字母顺序排序,因为单词是大写的。
例如:如果有小写的" aCid“和”aCid“,那么它将首先显示aCid,然后显示able,还有一种情况是,如果第一个字母是大写,它首先出现,例如,"able”和"a“。这里首先显示的是first。
发布于 2011-04-05 02:49:49
看看这里:Creating and Using Sort Descriptors
您可以将其比作不区分大小写。
NSSortDescriptor *sorter = [[[NSSortDescriptor alloc]
initWithKey:@"w"
ascending:YES
selector:@selector(localizedCaseInsensitiveCompare:)] autorelease];
NSArray *sortDescriptors = [NSArray arrayWithObject: sorter];
[mGlossaryArray sortUsingDescriptors:sortDescriptors];
发布于 2013-02-15 11:49:28
只需像我以前一样使用NSSortDescriptor,它就工作得很好。
NSSortDescriptor * sortByRank = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES selector:@selector(caseInsensitiveCompare:)];
发布于 2012-01-24 21:05:56
我可以建议使用-localizedStandardCompare:(NSString)吗?
“只要在列表和表中显示文件名或其他字符串,就应该使用此方法。此方法的确切排序行为在不同的区域设置下是不同的,在将来的版本中可能会有所更改。”
https://stackoverflow.com/questions/5542762
复制相似问题