问题描述
2 天前我问了一个问题,如何按字符串对象中的第二个单词对元素进行排序,
2 days ago I asked the question how to sort elements by second word in string object and
我得到了解决方案 这里
但现在我陷入了新的问题
but now I stuck into new issue
考虑我有
nsarray with--> 名称和
nsarray with--> names and
nsdictionary with same-->names(of nsarray) 作为值和电子邮件 id 作为键
nsdictionary with same-->names(of nsarray) as value and email id as key
我的 nsarray 包含
"Test Teacher"
"Anonymous"
"Dok Teacher"
和我的nsdictionary 包含
"Dok Teacher" --"a@g.com"
"Anonymous" -- "b@g.com"
"Test Teacher" --"c@g.com"
因为我需要对这些数组和字典进行排序,所以我使用上面链接中给出的方法对它们进行了排序
As I was needed to sort these array and dictionary i sorted them using method given in link above
但排序后我得到的数据如下
but after sorting I am getting data as follow
排序后的nsarray第二个词
"Anonymous"
"Test Teacher"
"Dok Teacher"
nsdictionary 排序后 值的第二个字
"Anonymous" -- "b@g.com"
"Dok Teacher" --"a@g.com"
"Test Teacher" --"c@g.com"
为什么使用相同的排序方法后,Dok Teacher 和 Test Teacher 在字典和 nsarray 中的不同位置.
why so Dok Teacher and Test Teacher at different postions in dictionary and in nsarray, after using same method for sorting.
以及如何在数组和字典中以相同的顺序获取它们.
And how to get them in same order for array and dictionary.
nsarray 排序代码:
code for sorting nsarray:
NSMutableArray *sortedArray = [NSMutableArray arrayWithArray:[teachersNames sortedArrayUsingComparator:^NSComparisonResult(id a, id b) {
NSString *firstTeacher2ndWord = [[[((NSString*)a) componentsSeparatedByString:@" "] objectAtIndex:1] lowercaseString];
NSString *secondTeacher2ndWord = [[[((NSString*)b) componentsSeparatedByString:@" "] objectAtIndex:1] lowercaseString];
return [firstTeacher2ndWord compare:secondTeacher2ndWord];
}]];
NSLog(@"After %@",sortedArray);
teachersSrc=[NSMutableArray arrayWithArray:sortedArray];
nsdictionary 排序代码
code for sorting nsdictionary
myArray = [teachersList keysSortedByValueUsingComparator:^NSComparisonResult(id a, id b) {
NSString *firstTeacher2ndWord = [[[((NSString*)a) componentsSeparatedByString:@" "] objectAtIndex:1] lowercaseString];
NSString *secondTeacher2ndWord = [[[((NSString*)b) componentsSeparatedByString:@" "] objectAtIndex:1] lowercaseString];
return [firstTeacher2ndWord compare:secondTeacher2ndWord];
}];
NSLog(@"After %@",myArray);
我也尝试过对数组和字典的名字进行排序,然后对两者的姓氏进行排序,但它根本不起作用,
i have also tried sorting on first name for both array and dictionary and then sorting on last name for both , but its not working at all,
推荐答案
鉴于你有这个:
NSArray *array = @[@"Test Teacher",@"Anonymous",@"Dok Teacher"];
NSDictionary *dictionary = @{@"a@g.com":@"Dok Teacher",@"b@g.com":@"Anonymous",@"c@g.com":@"Test Teacher"};
//sort the array first
NSArray *sortedArray = [array sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
NSString *firstTeacher2ndWord =[[(NSString *)obj1 componentsSeparatedByString:@" "]lastObject];
NSString *secondTeacher2ndWord =[[(NSString *)obj2 componentsSeparatedByString:@" "]lastObject];
if([firstTeacher2ndWord compare:secondTeacher2ndWord options:NSCaseInsensitiveSearch] == FALSE){ // means they are the same
//compare full text
firstTeacher2ndWord = (NSString *)obj1;
secondTeacher2ndWord = (NSString *)obj2;
}
return [firstTeacher2ndWord compare:secondTeacher2ndWord options:NSCaseInsensitiveSearch];
}];
NSLog(@"Sorted Array %@",sortedArray);
//sort the dictionary key using values
NSArray *sortedDictionaryKey =[dictionary keysSortedByValueUsingComparator:^NSComparisonResult(id obj1, id obj2) {
NSString *firstTeacher2ndWord =[[(NSString *)obj1 componentsSeparatedByString:@" "]lastObject];
NSString *secondTeacher2ndWord =[[(NSString *)obj2 componentsSeparatedByString:@" "]lastObject];
if([firstTeacher2ndWord compare:secondTeacher2ndWord options:NSCaseInsensitiveSearch] == FALSE){ // means they are the same
//compare full text
firstTeacher2ndWord = (NSString *)obj1;
secondTeacher2ndWord = (NSString *)obj2;
}
return [firstTeacher2ndWord compare:secondTeacher2ndWord options:NSCaseInsensitiveSearch]
}];
//get the values from the dictionary based on the sorted keys
NSLog(@"Sorted Dictionary %@",[dictionary objectsForKeys:sortedDictionaryKey notFoundMarker:[NSNull null]]);
会给你这个输出:
//Sorted Array
Sorted Array(
Anonymous,
"Dok Teacher",
"Test Teacher"
)
//Sorted Dictionary
Sorted Dictionary(
Anonymous,
"Dok Teacher",
"Test Teacher"
)
这篇关于对 nsdictionary 排序的结果与对 nsarray 排序的结果不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!