我如何排序一个数组充满了[UIFont familyNames]按字母顺序?
当前回答
对于大多数目的,这已经有了很好的答案,但我将添加我的更具体的答案。
在英语中,通常我们按字母顺序排列时,会忽略短语开头的单词“the”。所以“United States”应该在“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;
}
其他回答
-(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];
}
一个更强大的排序NSString列表的方法是使用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]];
这里提供的其他答案提到使用@selector(localizedCaseInsensitiveCompare:) 这对于NSString数组非常有效,但是如果你想将它扩展到另一种类型的对象,并根据'name'属性对那些对象进行排序,你应该这样做:
NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES];
sortedArray=[anArray sortedArrayUsingDescriptors:@[sort]];
您的对象将根据这些对象的name属性进行排序。
如果希望排序不区分大小写,则需要像这样设置描述符
NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES selector:@selector(caseInsensitiveCompare:)];
对于大多数目的,这已经有了很好的答案,但我将添加我的更具体的答案。
在英语中,通常我们按字母顺序排列时,会忽略短语开头的单词“the”。所以“United States”应该在“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;
}
使用以下代码按字母顺序排序:
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
)
推荐文章
- UICollectionView中的单元格间距
- 我如何在我的iOS应用程序中每n分钟得到一个后台位置更新?
- 就地基数排序
- 如何使用UIVisualEffectView来模糊图像?
- 从另一个列表id中排序一个列表
- 如何改变时间和时区在iPhone模拟器?
- 我如何知道何时UITableView完成了ReloadData?
- Python数据结构按字母顺序排序
- 如何用jquery或javascript排序对象数组
- 复制文本到剪贴板与iOS
- 排序10个数字的最快方法?(数字为32位)
- 什么是Swift相当于respondsToSelector?
- 如何按值排序计数器?python -
- 如何检查NSString是否以某个字符开始
- Java 8流反向顺序