How can I sort an Array of strings that contains numbers AND characters? My tableView has this Data in it:
- (void)viewDidLoad {
[super viewDidLoad];
self.tableData = [NSMutableArray array];
[self.tableData addObject:@"28 days"];
[self.tableData addObject:@"29 weeks"];
[self.tableData addObject:@"3 days"];
[self.tableData addObject:@"10 years"];
[self.tableData addObject:@"4 months"];
[self.tableData addObject:@"3 Weeks"];
[self.tableData addObject:@"27 Years"];
[self.tableData addObject:@"1 months"];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"Sort" style:UIBarButtonItemStyleBordered target:self action:@selector(sort)];}
And I would like to sort it in a "natural" way, where the days come first, then the weeks, the months etc. and at the same time 2 days be before 5 days etc. Is that possible? or do I have to create separate arrays for weeks, months, years etc.?
This is the code I used:
- (void)sort {
NSSortDescriptor *sorter = [[NSSortDescriptor alloc]initWithKey:nil ascending:YES selector:@selector(localizedCaseInsensitiveCompare:)];
NSArray *sortArray = [NSArray arrayWithObject:sorter];
NSArray *sortedArray = [self.tableData sortedArrayUsingDescriptors:sortArray];
[self.tableData removeAllObjects];
[self.tableData addObjectsFromArray:sortedArray];
[self.tableView reloadData];}
This doesn't solve the numerical problem though and somehow doesn't even sort the data in the right way ^^