gpt4 book ai didi

ios - 在 nsdictionary 上排序给出的结果与在 nsarray 上排序不同

转载 作者:行者123 更新时间:2023-11-29 03:21:39 25 4
gpt4 key购买 nike

2天前,我问了一个问题:如何按字符串对象中的第二个单词对元素进行排序

我得到了解决方案here

但现在我遇到了新问题

考虑我有

nsarray with--> 名称和

nsdictionary,以相同的名称(nsarray)作为值,电子邮件 ID 作为键

我的nsarray包含

   "Test Teacher"

"Anonymous"

"Dok Teacher"

我的nsdictionary包含

   "Dok Teacher" --"a@g.com"

"Anonymous" -- "b@g.com"

"Test Teacher" --"c@g.com"

由于需要对这些数组和字典进行排序,我使用上面链接中给出的方法对它们进行了排序

但排序后我得到的数据如下

在第二个单词上排序后的 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 中的位置不同。

以及如何以相同的顺序获取数组和字典。

对 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 进行排序的代码

    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);

编辑:

我还尝试过对数组和字典的名字进行排序,然后对两者的姓氏进行排序,但它根本不起作用,

最佳答案

鉴于你有这个:

 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"
)

关于ios - 在 nsdictionary 上排序给出的结果与在 nsarray 上排序不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21012740/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com