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

  • SEARCH
  • Home
  • 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 7884075
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T04:42:09+00:00 2026-06-03T04:42:09+00:00

I am having trouble trying to sort my NSDictionary in order, and it’s been

  • 0

I am having trouble trying to sort my NSDictionary in order, and it’s been weeks and I still haven’t figured it out, so I wish some one could give me a hand here…

NSDictionary data is from JSON and the data is already sorted from the server and it displayed in order in the JSON, but when it retrieved and converted to NSDicitionary, the order is all wrong…

This is the JSON

{ 
  "categories":{
   "unknownCategoryName":[
   { "key1":"value1",
     "key2":"value2"
   }],
   "unknownCategoryName1":[
   { "key1":"value1",
     "key2":"value2"
   }],
   "unknownCategoryName2":[
   { "key1":"value1",
     "key2":"value2"
   }],
   "unknownCategoryName3":[
   { "key1":"value1",
     "key2":"value2"
   }]
  }
 }

The category quantity and its name will not be known until the JSON is received, so this is what I am using to get the count and setting up the tableView and the section

NSDictionary *results = [jsonString JSONValue];

NSDictionary *all = [results objectForKey:@"categories"];
self.datasource = all;


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  {
   return [self.datasource count];
  }

 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
   NSString *title = [[self.datasource allKeys] objectAtIndex:section];
   return title;
 }

And what I want to do is to list out the section and the section title as the order in the JSON…. rather than a chaos display like

unknownCategory3
unknownCategory1
unknownCategory
unknownCategory2

The “unknownCategory” is Product Category name, they are not in alphabet order, without any number, and they are already sorted and display in order in the JSON…

So, it would be great if you guys could help.
Thanks in advance…

  • 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-06-03T04:42:10+00:00Added an answer on June 3, 2026 at 4:42 am

    Keys are not ordered in NSDictionaries, so you have to sort the keys first before using them in your table view. So, instead of using [self.datasource allKeys] to get your section titles, first create a sorted array: sorted = [[self.datasource allKeys] sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)], and then use that array to get the titles: title = [sorted objectAtIndex:section].

    After Edit to answer a further question:

    To use the sorted array to get the values you want into your table, I think this should be close. You would have to add a property, NSArray *sorted, to your .h file, and then this in your .m (this is assuming the structure of your json is as you posted above):

     NSDictionary *results = [jsonString JSONValue];
        NSDictionary *all = [results objectForKey:@"categories"];
        self.datasource = all;
        self.sorted = [[self.datasource allKeys] sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
    
        - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
            return [self.sorted count];
        }
    
        - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
            NSString *title = [self.sorted objectAtIndex:section];
            return title;
        }
    
       - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
            return [[self.datasource valueForKey:[self.sorted objectAtIndex:section]]count];
       }
    
        - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
            static NSString *cellIdentifier = @"Cell";
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
            if (cell == nil) {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
            }
            NSArray *theArray = [self.datasource valueForKey:[self.sorted objectAtIndex:indexPath.section]];
            NSString *text1 = [[theArray objectAtIndex:0] valueForKey:@"key1"];
            NSString *text2 = [[theArray objectAtIndex:0] valueForKey:@"key2"];
            cell.textLabel.text = [NSString stringWithFormat:@"%@\r%@",text1,text2];
            cell.textLabel.numberOfLines=0; // this set the number of lines to unlimited
            return cell;
        }
    

    You didn’t say how you wanted to display two different values in one table row — in this example, I concatenated them into one string with a return between the two.

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

Sidebar

Related Questions

Im having trouble trying to figure out some code someone has suggested and was
ive been trying to sort an array in desc order but have been having
I am really having trouble trying to figure out how to find certain records
Hey all, I'm trying to write a sort function but am having trouble figuring
I'm having bit of a trouble trying to figure out how I would send
I'm having trouble trying to sort a arrayref of hashes based on a value
i have been trying to understand the pack function. I'm having trouble on this
I am trying to order a database by date but I am having trouble
I'm having trouble replacing some url variables in javascript. I'm trying to replace the
thanks for taking the time out to read this post. I'm having trouble trying

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.