0

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 objectAtIndex:indexPath.row-1] lastPathComponent];

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

cell.textLabel.text = filenameWithoutExtension;
Community
  • 1
  • 1
Heena
  • 754
  • 5
  • 18
  • 30
  • 2
    Maybe this [link][1] would help. [1]: http://stackoverflow.com/questions/1351182/how-to-sort-a-nsarray-alphabetically – mayuur Sep 02 '11 at 05:04

4 Answers4

3

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:)];
Liz
  • 171
  • 4
2
sortedArray = [yourArray sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
gcamp
  • 14,622
  • 4
  • 54
  • 85
PJR
  • 13,052
  • 13
  • 64
  • 104
1

Check this

And also this

Community
  • 1
  • 1
1

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).

gcamp
  • 14,622
  • 4
  • 54
  • 85
dtuckernet
  • 7,817
  • 5
  • 39
  • 54