我想使用NSSortDescriptor对数组进行排序

我想使用NSSortDescriptor对数组进行排序

本文介绍了我想使用NSSortDescriptor对数组进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在排序带有w.r.t数据库的数组时遇到问题:

I am having a problem regarding sorting an array w.r.t database:

NSSortDescriptor *sorter = [[NSSortDescriptor alloc] initWithKey:@"w" ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObject: sorter];

[mGlossaryArray sortUsingDescriptors:sortDescriptors];
[sorter release];

数据库中有一些首字母大写,并且由于该大写字母而无法显示正确的排序输出.在这里,我正在排序数组r.t"w",这是我数据库中的表列.在这里,我附上了输出的屏幕截图,该屏幕截图显示"Cancer"比"c"优先,但这是不正确的,因为大写单词没有按字母顺序排序.

Here in database there are some first capital letters and because of that capital letter it does not show me proper sorted output. Here i am sorting an array with r.t "w" which is my table column in database.Here I have attach the screen shot for the output, which says that "Cancer" comes first than "c", but this is not correct, it is not giving alphabetically sort because of the capitalized words.

例如如果有小写的"able"和小写的"aCid",那么它将先显示aCid,然后才有能力,并且还有一种情况,如果第一个字母为大写,则首先显示,例如"Able"和"a".此处首先显示Able.

eg. if there is "able" in lower case and "aCid" then it will show aCid first and then able, and there is also a case where if the 1st letter is caps it comes first eg, "Able" and "a". Here Able displays first.

推荐答案

在这里看看:创建和使用排序描述符

您可以比较不区分大小写.

You can compare as case-insensitive.

NSSortDescriptor *sorter = [[[NSSortDescriptor alloc]
          initWithKey:@"w"
          ascending:YES
          selector:@selector(localizedCaseInsensitiveCompare:)] autorelease];
NSArray *sortDescriptors = [NSArray arrayWithObject: sorter];
[mGlossaryArray sortUsingDescriptors:sortDescriptors];

这篇关于我想使用NSSortDescriptor对数组进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 17:26