Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6596541
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:05:22+00:00 2026-05-25T18:05:22+00:00

After searching the developer docs, google and other sites I still can’t find how

  • 0

After searching the developer docs, google and other sites I still can’t find how to accomplish the following…

I would like to sort a NSDictionary by the amount of values (i.e. count) per key. Do I need to use a custom NSSortDescriptor and if so, I would really appreciate some help with the code. Below is an example of the NSDictionary I would like sorted.

2 ways I would like to be able to sort dictionary:

1) Sort it so that the key “Australia” comes first because it has 3 cities, then USA with 2 cities, and finally UK with 1 city?

2) For a bonus I would also love to have the option to sort by the amount of objects i.e. Australia first with 6 objects, then UK with 4 objects (even though only 1 city) then USA with 2 objects.

"United Kingdom" = {
    "City 1" = {
        1 = "Object A";
        2 = "Object B";
        3 = "Object C";
        4 = "Object D";
    };
};
"Australia" = {
    "City 1" = {
        5 = "Object E";
        6 = "Object F";
        7 = "Object G";
    };
    "City 2" = {
        8 = "Object H";
        9 = "Object I";
    };
    "City 3" = {
        10 = "Object J";
    };
};
"United States of America" = {
    "City 1" = {
        11 = "Object K";
    };
    "City 2" = {
        12 = "Object L";
    };
};
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T18:05:23+00:00Added an answer on May 25, 2026 at 6:05 pm

    Assuming you do store arrays in the dictionary you could do something like this:

    NSComparator sorter = ^NSComparisonResult(id a, id b)
    {
        NSArray* a1 = a;
        NSArray* a2 = b;
        if([a1 count] > [a2 count]) return NSOrderedAscending; 
        if([a1 count] < [a2 count]) return NSOrderedDescending; 
        return NSOrderedSame;
    }
    
    NSArray* ordered = [dictionary keysSortedByValueUsingComparator:sorter];
    

    Then in the “ordered” array you’ll get the keys to the dictionary in the order of increasing number of elements in the arrays stored in the dictionary.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I cannot find this documented anywhere and after looking over Google's api docs for
After some searching on Google and here, I still haven't found any answer to
After searching SO and other sites, I've failed to come up with conclusive evidence
After searching around i still have no clue about the best practice to reorder
After searching online, the best solution I've found so far is to just make
After searching online and going through past stackoverflow posts for a suitable implementation for
After searching through some existing libraries for JSON, I have finally ended up with
After searching a lot i did not get any answers and finally i had
After searching a long time for a performance bug, I read about denormal floating
First attempt to use this cool site - after searching for 2 hours: So

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.