开发者

How to sort NSArray? [duplicate]

This question already has answers here: Closed 11 years ago.

Possible Duplicate:

How to sort a NSArray alphabetically?

I am using below code to load data into UITableView from NSArray.

How can I sort them in alphabetical order ? Mine is NSArray not NSMutableArray nor I am using Dictionary

defaultVot = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:pathToVot error:&error];

NSString *fileName = [[defaultVot obje开发者_如何学CctAtIndex:indexPath.row-1] lastPathComponent];

NSString *filenameWithoutExtension = [fileName substringToIndex:[fileName length] -4];

cell.textLabel.text = filenameWithoutExtension;


You should probably sort your array earlier. (Likely when you load the list.) Then use the sorted array as the datasource.

NSArray * defaultVot = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:pathToVot error:&error];
NSMutableArray * array = [NSMutableArray arrayWithCapacity:[defaultVot count]];
for (NSString * f in defaultVot){
    NSString *fileName = [f lastPathComponent];
    NSString *filenameWithoutExtension = [fileName substringToIndex:[fileName length] -4];
    [array addObject:filenameWithoutExtension];
}
NSArray * sortedArray = [array sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];


sortedArray = [yourArray sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];


Check this

And also this


You can see Apple's documentation on how to accomplish this.

As an example:

// From the Documentation
sortedArray =
    [anArray sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];

This will return a new array that is sorted using the rules for the current locale (in a case insensitive manner).

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜