對不起,這個令人費解的做法,但這樣做的工作......
NSArray *rowTitleArray = [[NSArray alloc] initWithObjects:
@"10. Tenth",
@"15. Fifteenth",
@"13. Thirteenth",
@"1. First",
@"2. Second",
@"22. TwentySecond", nil];
NSMutableArray *dictionaryArray = [NSMutableArray array];
for (NSString *original in rowTitleArray) {
NSString *numberString = [[original componentsSeparatedByString:@"."] objectAtIndex:0];
NSNumber *number = [NSNumber numberWithInt:[numberString intValue]];
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
number, @"number", original, @"rowTitle", nil];
[dictionaryArray addObject:dict];
}
NSSortDescriptor *descriptor = [NSSortDescriptor sortDescriptorWithKey:@"number" ascending:YES];
NSArray *sortedDictionaryArray = [dictionaryArray sortedArrayUsingDescriptors:
[NSArray arrayWithObject:descriptor]];
NSMutableArray *sortedRowTitles = [NSMutableArray array];
for (NSDictionary *dict in sortedDictionaryArray) {
[sortedRowTitles addObject:[dict objectForKey:@"rowTitle"]];
}
rowTitleArray = [NSArray arrayWithArray:sortedRowTitles];
NSLog(@"%@", rowTitleArray);
輸出:
"1. First",
"2. Second",
"10. Tenth",
"13. Thirteenth",
"15. Fifteenth",
"22. TwentySecond"
我會盡量想一個更優雅的解決方案。
你是如何在這個數據加載/什麼類型的結構是持久數據存儲(即plist出現nsdictionary/nsarray或核心數據) – Kaiser
數組排序看起來正確。 '10'在'1.'之後和'2'之前排序。請記住,計算機不會像您一樣識別號碼。 –
好的,但我怎麼能按照我想要的方式做到這一點? @邁克爾其plist :) –