Iphone 我想使用NSSortDescriptor对数组进行排序
我在排序数组w.r.t数据库时遇到问题:Iphone 我想使用NSSortDescriptor对数组进行排序,iphone,nsarray,jquery-ui-sortable,sorting,Iphone,Nsarray,Jquery Ui Sortable,Sorting,我在排序数组w.r.t数据库时遇到问题: NSSortDescriptor *sorter = [[NSSortDescriptor alloc] initWithKey:@"w" ascending:YES]; NSArray *sortDescriptors = [NSArray arrayWithObject: sorter]; [mGlossaryArray sortUsingDescriptors:sortDescriptors]; [sorter release]; 在数据库
NSSortDescriptor *sorter = [[NSSortDescriptor alloc] initWithKey:@"w" ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObject: sorter];
[mGlossaryArray sortUsingDescriptors:sortDescriptors];
[sorter release];
在数据库中,有一些首字母大写,由于那个大写字母,它不能显示正确的排序输出。这里我用r.t“w”对一个数组进行排序,这是我在数据库中的表列。
这里我附上了输出的屏幕截图,上面说“癌症”比“c”排在第一位,但这是不正确的,因为大写的单词没有按字母顺序排序
如果小写字母中有“able”和“aCid”,那么它将首先显示aCid,然后显示able,还有一种情况是,如果第一个字母是caps,那么它将首先显示,例如,“able”和“a”。首先在此处显示Able。请查看此处:
您可以比较为不区分大小写
NSSortDescriptor *sorter = [[[NSSortDescriptor alloc]
initWithKey:@"w"
ascending:YES
selector:@selector(localizedCaseInsensitiveCompare:)] autorelease];
NSArray *sortDescriptors = [NSArray arrayWithObject: sorter];
[mGlossaryArray sortUsingDescriptors:sortDescriptors];
我想这会对你有好处的。其文档如下所示: 添加Apple编写的这个小函数
int finderSortWithLocale(id string1, id string2, void *locale)
{
static NSStringCompareOptions comparisonOptions =
NSCaseInsensitiveSearch | NSNumericSearch |
NSWidthInsensitiveSearch | NSForcedOrderingSearch;
NSRange string1Range = NSMakeRange(0, [string1 length]);
return [string1 compare:string2
options:comparisonOptions
range:string1Range
locale:(NSLocale *)locale];
}
请确保将函数定义复制到标头中,否则会在排序数组中出现编译错误
对于排序数组,请使用以下方法:
[mGlossaryArray sortedArrayUsingFunction:finderSortWithLocale context:[NSLocale currentLocale]];
您的结果如下所示:
- c
- 船舱
- 咖啡馆
- 癌症
- 中国人
- 基督教
- 圣诞节
- 可乐
“每当文件名或其他字符串出现在列表和表格中时,如果类似查找器的排序是合适的,则应使用此方法。此方法的确切排序行为在不同的地区有所不同,可能会在未来的版本中更改。”此代码对我来说运行良好
- (void)sortSearchResultWithInDocumentTypeArray:(NSMutableArray *)aResultArray basedOn:(NSString *)aSearchString {
NSSortDescriptor * frequencyDescriptor =[[NSSortDescriptor alloc] initWithKey:aSearchString ascending:YES comparator:^(id firstDocumentName, id secondDocumentName) {
static NSStringCompareOptions comparisonOptions =
NSCaseInsensitiveSearch | NSNumericSearch |
NSWidthInsensitiveSearch | NSForcedOrderingSearch;
return [firstDocumentName compare:secondDocumentName options:comparisonOptions];
}];
NSArray * descriptors = [NSArray arrayWithObjects:frequencyDescriptor, nil];
[aResultArray sortUsingDescriptors:descriptors];
}
您可以使用它根据名称对数组进行排序,该名称也包含小写字母:
NSSortDescriptor *sorter = [NSSortDescriptor sortDescriptorWithKey:@"w" ascending:YES selector:@selector(caseInsensitiveCompare:)];
NSArray *sortDescriptors = [NSArray arrayWithObject:sorter];
[mGlossaryArray sortUsingDescriptors:sortDescriptors];
这段代码对我来说很好,可以根据字母表对名称进行排序,字母表中也有小字符,如rocky、Ajay、john、Bob等。只需像我使用的那样使用NSSortDescriptor,它就工作得很好了
NSSortDescriptor * sortByRank = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES selector:@selector(caseInsensitiveCompare:)];
Apple的finder sort with locale方法的另一种形式是使用comparator块,如果您处于ARC环境中,并且不想处理桥接转换等问题,这将非常有用:
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"your_string_key" ascending:YES comparator:^NSComparisonResult(id obj1, id obj2) {
NSStringCompareOptions comparisonOptions = NSCaseInsensitiveSearch | NSNumericSearch | NSWidthInsensitiveSearch | NSForcedOrderingSearch;
NSRange string1Range = NSMakeRange(0, ((NSString *)obj1).length);
return [(NSString *)obj1 compare: (NSString *)obj2 options: comparisonOptions range: string1Range locale: [NSLocale currentLocale]];
}];
NSArray *sortedArray = [originalArray sortedArrayUsingDescriptors:@[sortDescriptor]];
为了提高效率,我也建议将当前语言环境存储在局部变量中。但它仍然没有排序它先给我所有的大写字母,然后给我小写字母你能给一些你想要的排序样本吗?看看图像显示,它给我未排序的输出,就好像在本例中是“c”应该是第一位的,但是癌症是第一位的。所以请帮我解决这个问题。我也有同样的问题,但给出的解决方案在我的情况下效果很好。完美的答案。“localizedCaseInsensitiveCompare”确保以外来字符(如法语e-acute)开头的字符串在列表中也能正确排序,并能很好地处理大小写。