0

I have a dictionary which contain this data:

    (
contact={name="Lion",id="1",photo="simba.png",address="elm street"},
    {name="Cat",id="2",photo="halleberry.png",address="attic"},
    {name="Bat",id="3",photo="dracule.jpg",address="long way home baby"}
    )

From that NSDictionary, i grab only the name and sorted it alphabetically. Like this:

(B={"Bat"}, C={"Cat"}, L={"Lion"})

This is the code i used:

NSMutableDictionary* sortedDict = [NSMutableDictionary dictionary];
 for (NSDictionary* animal in dataDict[@"user"]){
    NSString* name = animal[@"name"];
    if (![name length])
       continue;
     NSRange range = [name rangeOfComposedCharacterSequenceAtIndex:0];
     NSString* key = [[name substringWithRange:range] uppercaseString];
     NSMutableArray* list = sortedDict[key];
     if (!list){
        list = [NSMutableArray array];
        [sortedDict setObject:list forKey:key];
     }
     [list addObject:name];

Then, what i want to ask is. What i need to create an array of photos but sorted alphabetically based on the name. I mean something like this:

(B="dracule.jpg", C="halleberry.png"...etc)

I also heard that this will be more effective to use (B={name="Bat", photo="draggle.jpg"}) but don't know how i can make something like this and don't know how to call it separately. Please i need your help :"(

5
  • I don't understand the data you present. It starts with (, which implies an array, and yet has a key of contact with a value of a dictionary but then there are more dictionaries following that. Please describe your data better. Commented Aug 28, 2014 at 9:31
  • Sorry, maybe i made some mistake because i type it not copy and paste but both are NSDictionary Commented Aug 28, 2014 at 9:34
  • So where are the keys for the 2nd and 3rd element of the top-level dictionary? Commented Aug 28, 2014 at 9:44
  • name, id, photo, address is all key Commented Aug 28, 2014 at 9:46
  • OK nevermind, I don't seem to be able to get through to you. Commented Aug 28, 2014 at 9:48

1 Answer 1

1

You can easily sort the array which contains dictionaries values, see below

//Get the contact array.

NSArray *contacts=[dic objectForKey:@"contact"];

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];

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

I hope it helps.

Sign up to request clarification or add additional context in comments.

3 Comments

If i make it like this, it only sorted the names, but i also need the initial to make an index @iphonic
@Edward Do you want indexing of the sorted data, or just indexing, or indexing first then sort indexed data ?
indexing first then sort the data. I want to make something like this

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.