在nsdictionary上排序的结果与在nsarray上排序的结果不同

时间:2014-01-09 06:05:08

标签: ios objective-c sorting nsarray nsdictionary

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教师和测试老师在使用相同的排序方法后,在字典和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);

修改

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

3 个答案:

答案 0 :(得分:1)

鉴于你有这个:

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

答案 1 :(得分:0)

首先,字典(teachersList)的值数组是未排序的,你不知道元素的排列方式。

第二个单词的Dok TeacherTest Teacher是相同的,它们之间没有顺序,排序后它们的顺序取决于排序方法(稳定或不稳定)和顺序原始数组。

如果值数组中的元素在teachersNames中具有相同的顺序,则结果将相同。否则,Dok TeacherTest Teacher的顺序未定义。

注意:此处的值数组为teachersList.allValues

答案 2 :(得分:0)

代码中的问题是第二个名称Teacher包含两个字。所以当你在数组中排序相同时。它将按字母顺序对名称进行排序。由于第二个名称对于两个单词是相同的。所以它不会反映在输出中。但如果您更改订单,则可以看到预期的输出。我已经在下面实现了对数组进行排序的代码: -

执行以下代码时,您将获得输出: -

  NSArray *array1=@[@"Test Teacher",@"Anonymous",@"Dok Teacher"];
NSArray *sortedArray=[array1 sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
    return [obj1 compare:obj2 options:NSCaseInsensitiveSearch];
}];
NSLog(@"%@",sortedArray);

输出: -

 Anonymous,
 "Dok Teacher",
 "Test Teacher"