如何按字母顺序排列NSArray?
我怎样才能按照字母顺序排列填充[UIFont familyNames]
的数组?
最简单的方法是提供一个sortingselect器(详细的苹果文档 )
Objective-C的
sortedArray = [anArray sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
迅速
let descriptor: NSSortDescriptor = NSSortDescriptor(key: "YourKey", ascending: true, selector: "localizedCaseInsensitiveCompare:") let sortedResults: NSArray = temparray.sortedArrayUsingDescriptors([descriptor])
苹果提供了几个字母sortingselect器:
-
compare:
-
caseInsensitiveCompare:
-
localizedCompare:
-
localizedCaseInsensitiveCompare:
-
localizedStandardCompare:
迅速
var students = ["Kofi", "Abena", "Peter", "Kweku", "Akosua"] students.sort() print(students) // Prints "["Abena", "Akosua", "Kofi", "Kweku", "Peter"]"
参考
在这里提供的其他答案提到使用@selector(localizedCaseInsensitiveCompare :)这对NSString数组非常有用,但是如果你想扩展到另一种types的对象,并根据'name'属性对这些对象进行sorting,你应该而是:
NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES]; sortedArray=[anArray sortedArrayUsingDescriptors:@[sort]];
您的对象将根据这些对象的名称属性进行sorting。
如果你想sorting是不区分大小写的,你需要像这样设置描述符
NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES selector:@selector(caseInsensitiveCompare:)];
对NSString列表进行sorting以使用类似NSNumericSearch的更强大的方法:
NSArray *sortedArrayOfString = [arrayOfString sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) { return [(NSString *)obj1 compare:(NSString *)obj2 options:NSNumericSearch]; }];
与SortDescriptor结合,可以得到如下的结果:
NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES comparator:^NSComparisonResult(id obj1, id obj2) { return [(NSString *)obj1 compare:(NSString *)obj2 options:NSNumericSearch]; }]; NSArray *sortedArray = [anArray sortedArrayUsingDescriptors:[NSArray arrayWithObject:sort]];
使用下面的代码按字母顺序sorting:
NSArray *unsortedStrings = @[@"Verdana", @"MS San Serif", @"Times New Roman",@"Chalkduster",@"Impact"]; NSArray *sortedStrings = [unsortedStrings sortedArrayUsingSelector:@selector(compare:)]; NSLog(@"Unsorted Array : %@",unsortedStrings); NSLog(@"Sorted Array : %@",sortedStrings);
以下是控制台日志:
2015-04-02 16:17:50.614 ToDoList[2133:100512] Unsorted Array : ( Verdana, "MS San Serif", "Times New Roman", Chalkduster, Impact ) 2015-04-02 16:17:50.615 ToDoList[2133:100512] Sorted Array : ( Chalkduster, Impact, "MS San Serif", "Times New Roman", Verdana )
对string数组进行sorting的另一种简单方法是使用NSString description
属性,方法如下:
NSSortDescriptor *valueDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"description" ascending:YES]; arrayOfSortedStrings = [arrayOfNotSortedStrings sortedArrayUsingDescriptors:@[valueDescriptor]];
-(IBAction)SegmentbtnCLK:(id)sender { [self sortArryofDictionary]; [self.objtable reloadData];} -(void)sortArryofDictionary { NSSortDescriptor *sorter; switch (sortcontrol.selectedSegmentIndex) {case 0: sorter=[[NSSortDescriptor alloc]initWithKey:@"Name" ascending:YES]; break; case 1: sorter=[[NSSortDescriptor alloc]initWithKey:@"Age" ascending:YES]; default: break; } NSArray *sortdiscriptor=[[NSArray alloc]initWithObjects:sorter, nil]; [arr sortUsingDescriptors:sortdiscriptor]; }
这对于大多数目的已经有了很好的答案,但是我会join更具体的我的。
在英语中,通常当我们拼写时,我们忽略短语开头的单词“the”。 所以“美国”将在“U”而不是“T”下命令。
这是为你做的。
最好把这些分类。
// Sort an array of NSStrings alphabetically, ignoring the word "the" at the beginning of a string. -(NSArray*) sortArrayAlphabeticallyIgnoringThes:(NSArray*) unsortedArray { NSArray * sortedArray = [unsortedArray sortedArrayUsingComparator:^NSComparisonResult(NSString* a, NSString* b) { //find the strings that will actually be compared for alphabetical ordering NSString* firstStringToCompare = [self stringByRemovingPrecedingThe:a]; NSString* secondStringToCompare = [self stringByRemovingPrecedingThe:b]; return [firstStringToCompare compare:secondStringToCompare]; }]; return sortedArray; } // Remove "the"s, also removes preceding white spaces that are left as a result. Assumes no preceding whitespaces to start with. nb: Trailing white spaces will be deleted too. -(NSString*) stringByRemovingPrecedingThe:(NSString*) originalString { NSString* result; if ([[originalString substringToIndex:3].lowercaseString isEqualToString:@"the"]) { result = [[originalString substringFromIndex:3] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]; } else { result = originalString; } return result; }