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 6604761
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:11:59+00:00 2026-05-25T19:11:59+00:00

Okay so I have been working through an example that closely matches what I

  • 0

Okay so I have been working through an example that closely matches what I am trying to achive, the sole difference being that in the example he is directly calling from his database the data he needs to be sectioned etc. Where as I already have a sorted NSArray.

This is the tutorial I am working off – iPhone Development: Creating Native Contacts like screen

I have created a Method that is capturing each entry in the NSArray and putting these results into a alpha based NSDictionary (so their will be a NSDictionary for A,B,C… etc)

here is my method.

//method to sort array and split for use with uitableview Index
- (IBAction)startSortingTheArray:(NSMutableArray *)arrayData
{
    //Sort incoming array alphabetically
    //sortedArray = [arrayData sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
    [self setSortedArray:[arrayData sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)]];

    arrayOfCharacters = [[NSMutableArray alloc]init];
    objectsForCharacters = [[NSMutableDictionary alloc]init];

    for(char c='A';c<='Z';c++)
    {

        if([sortedArray count] >0)
        {
            [arrayOfCharacters addObject:[NSString stringWithFormat:@"%c",c]];
            [objectsForCharacters setObject:sortedArray forKey:[NSString stringWithFormat:@"%c",c]];
            NSLog(@"%@", objectsForCharacters);
        }
        [sortedArray release];


    //Reloads data in table
    [self.tableView reloadData];
    }
}

This is putting every value into every alpha section, I am hoping someone can help me with making it so that only alpha sections are established if there is a value in the array for it.. then only loading those values into each section, not every section.

  • 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-25T19:11:59+00:00Added an answer on May 25, 2026 at 7:11 pm

    This piece of code will do just that and will be much more efficient than filtering the array once for each letter.

    //Sort incoming array alphabetically so that each sub-array will also be sorted.
    NSArray *sortedArray = [arrayData sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
    
    // Dictionary will hold our sub-arrays
    NSMutableDictionary *arraysByLetter = [NSMutableDictionary dictionary];
    
    // Iterate over all the values in our sorted array
    for (NSString *value in sortedArray) {
    
        // Get the first letter and its associated array from the dictionary.
        // If the dictionary does not exist create one and associate it with the letter.
        NSString *firstLetter = [value substringWithRange:NSMakeRange(0, 1)];
        NSMutableArray *arrayForLetter = [arraysByLetter objectForKey:firstLetter];
        if (arrayForLetter == nil) {
            arrayForLetter = [NSMutableArray array];
            [arraysByLetter setObject:arrayForLetter forKey:firstLetter];
        }
    
        // Add the value to the array for this letter
        [arrayForLetter addObject:value];
    }
    
    // arraysByLetter will contain the result you expect
    NSLog(@"Dictionary: %@", arraysByLetter);
    

    Note that arraysByLetter is a dictionary that contains one array per “first letter” that exists in your initial data.

    — Added on 2011-09-23 —

    [sortedArray removeAllObjects];
    NSArray *sortedKeys = [arraysByLetter.allKeys sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
    
    for (NSString *key in sortedKeys) {
        [sortedArray addObject:key];
        [sortedArray addObjectsFromArray: [arraysByLetter objectForKey:key]];
    }
    
    NSLog(@"Sorted Array: %@", sortedArray);
    

    The output is the following:

    C,
    Computer,
    H,
    Helene,
    Hello,
    J,
    Jules,
    W,
    World
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to cakephp so have just been working through the cookbook and am
Okay, I'm working on a problem that I've been holding off on for three
Okay I have been working on a website but I keep on having an
Okay, so I have been using a PDO wrapper for a project I'm working
Okay I've been trying to figure this out for a while now. I have
Okay guys I have a database I have been working on for days now.
Okay so i have been working on this app for a couple months now
Okay I have been working on this for about 2 hours and am sure
Okay this one has me stumped.. mainly because i have been working on this
Okay, so far, I have been taking computer science courses in my high school

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.