-1

I need to sort the given "document" array with "lastname". Here is the sample json

document [
{
    id:"100"
    person :{
       name : {
        firstname : "xyz",
        lastname  : "oops"
       },
       photourl: "photo-url"
    }
},
{
    id:"200"
    person :{
       name : {
        firstname : "xyz",
        lastname  : "oops"
       },
       photourl: "photo-url"
    }
}]

it has an dict "name" inside one more dict "person" and we need to sort with lastname first n then with lastname

Ghouse
  • 3
  • 2

5 Answers5

1
NSSortDescriptor *lastNameDescriptor = [[NSSortDescriptor alloc] initWithKey:@"lastname " ascending:YES] ;
NSArray *sortedArray = [NSArray arrayWithObject:lastNameDescriptor];
sortedArray = [myArray sortedArrayUsingDescriptors:sortDescriptors];

for more clear description and code check this link, its very easy and perfect, Cheers :)

iAhmed
  • 6,556
  • 2
  • 25
  • 31
  • 1
    This does not compile (because sortDescriptors is nowhere defined) and is *not correct*, because "lastname" is a key inside the "person" dictionay. – Martin R Jul 10 '13 at 12:44
  • 1
    ***I*** don't want anything. I just noticed that the answer is wrong. – Martin R Jul 10 '13 at 13:26
  • its just a hint brother. use link below it will surely help you. i have also done by same link. – iAhmed Jul 10 '13 at 14:44
0

So assuming you put your JSON into an array called "people":

NSArray *people = myArrayFromJSON;

NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"person" ascending:YES comparator:^NSComparisonResult(id obj1, id obj2) {
    NSString *lastName1 = [obj1 valueForKey:@"lastname"];
    NSString *lastname2 = [obj2 valueForKey:@"lastname"];

    return [lastName1 compare:lastname2];
}];

NSArray *sortedArray = [people sortedArrayUsingDescriptors:@[sortDescriptor]];

That'll do it.

0

Use the below given logic

for (NSMutableDictionary *document in documents) {
    NSString *lastName = [[document valueForKey:@"person"] valueForKey:@"lastname"];
    [documents setObject:lastName forKey:@"lastnameDescriptor"];
}

NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:@"lastnameDescriptor" ascending:YES];
NSMutableArray *sortedNames = [documents sortedArrayUsingDescriptors:[NSArray arrayWithObject:descriptor]];

for (NSMutableDictionary *document in sortedNames) {
    [document removeObjectForKey:@"lastnameDescriptor"];
}
Loganathan
  • 1,697
  • 1
  • 13
  • 17
0

The other answers are close but either needlessly complicated or a little off.

NSSortDescriptor *descriptor = [NSSortDescriptor sortDescriptorWithKey:@"person.lastName" ascending:YES];
sortedArray = [document sortedArrayUSingDescriptors:@[descriptor]];

(The selector says "key," but if you read the docs, it's actually a keypath, so this will work as expected.)

Chuck
  • 234,037
  • 30
  • 302
  • 389
0

Lots of almost right answers but none that consider that 'name' is itself a nested dictionary in your document. So I think what you want is:

NSSortDescriptor *lastNameDescriptor = [NSSortDescriptor sortDescriptorWithkey:@"person.name.lastname" ascending:YES];
NSSortDescriptor *firstNameDescriptor = [NSSorteDescriptor sortDescriptorWithKey:@"person.name.firstname" ascending:YES];
NSArray *sortedArray = [document sortedArrayUsingDescriptors:@[lastNameDescriptor, firstNameDescriptor]];
ehope
  • 506
  • 3
  • 9
  • Thanks Erik H. this code worked. but how come string "person.name.lastname" is compared with dictionary keypath value – Ghouse Jul 12 '13 at 11:32