ViruMax
ViruMax

Reputation: 1218

sort on nsdictionary is giving different result than sort on nsarray

2 days ago I asked the question how to sort elements by second word in string object and

i got solution here

but now I stuck into new issue

Consider I have

nsarray with--> names and

nsdictionary with same-->names(of nsarray) as value and email id as key

And my nsarray contains

   "Test Teacher"

   "Anonymous"

   "Dok Teacher"

and my nsdictionary contains

   "Dok Teacher" --"[email protected]"

   "Anonymous" --  "[email protected]"

   "Test Teacher" --"[email protected]"

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 after sorting on second word

   "Anonymous"

   "Test Teacher"

   "Dok Teacher"

nsdictionary after sorting on second word of values

   "Anonymous" --  "[email protected]"

   "Dok Teacher" --"[email protected]"

   "Test Teacher" --"[email protected]"

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.

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

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

EDIT:

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,

Upvotes: 0

Views: 390

Answers (3)

Hussain Shabbir
Hussain Shabbir

Reputation: 15015

The problem in your code is second name Teacher contains in two words. So when you sort the same in array. It will sort the name alphabetically. Since the second name are same for two words. So it will not reflect in the output. But if you change the order then you can see the expected output. I have implemented the code for sorting an array below:-

When execute this code below you will get the output:-

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

Output:-

 Anonymous,
 "Dok Teacher",
 "Test Teacher"

Upvotes: 0

Joshua
Joshua

Reputation: 2451

Given that you have this:

 NSArray *array = @[@"Test Teacher",@"Anonymous",@"Dok Teacher"];
 NSDictionary *dictionary = @{@"[email protected]":@"Dok Teacher",@"[email protected]":@"Anonymous",@"[email protected]":@"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]]);

will give you this output:

//Sorted Array
Sorted Array(
Anonymous,
"Dok Teacher",
"Test Teacher"
)

//Sorted Dictionary
Sorted Dictionary(
    Anonymous,
    "Dok Teacher",
    "Test Teacher"
  )

Upvotes: 1

KudoCC
KudoCC

Reputation: 6952

First the value array of The dictionary(teachersList) is unsorted and you don't know how the element in are arranged.

Second Dok Teacher and Test Teacher is the same on the second word, there is no order between them, the order of them after sorting is depended on the sorting method(stable or unstable) and the order of the original array.

If elements in the value array have the same order in teachersNames, the result will be the same. Otherwise the order of Dok Teacher and Test Teacher is undefined.

Note: value array here is teachersList.allValues

Upvotes: 0

Related Questions