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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T21:30:47+00:00 2026-05-31T21:30:47+00:00

I have a sorted mutable array of a class called Topic. The topics represent

  • 0

I have a sorted mutable array of a class called Topic. The topics represent a an array of Publications. I present the topics in a table, and periodically fetch new publications from a web service. When a new publication arrives, I’d like to add to the table with an animation.

What’s bothering me is the computational work I need to do to add into this array, and answer the correct index path. Can someone suggest a more direct way than this:

// add a publication to the topic model.  if the publication has a new topic, answer
// the index path of the new topic
- (NSIndexPath *)addPublication:(Publication *)pub {

    // first a search to fit into an existing topic
    NSNumber *topicId = [pub valueForKey:@"topic_id"];
    for (Topic *topic in self.topics) {
        if ([topicId isEqualToNumber:[topic valueForKey:"id"]]) {
            // this publication is part of an existing topic, no new index path
            [topic addPublication:pub];
            return nil;
         }
    }

    // the publication must have a new topic, add a new topic (and therefore a new row)
    Topic *topic = [[Topic alloc] initWithPublication:publication];
    [self.topics addObject:topic];

    // sort it into position
    [self.topics sortUsingSelector:@selector(compareToTopic:)];

    // oh no, we want to return an index path, but where did it sort to?
    // yikes, another search!
    NSInteger row = [self.topics indexOfObject:topic];
    return [NSIndexPath indexPathForRow:row inSection:0];
}

// call this in a loop for all the publications I fetch from the server,
// collect the index paths for table animations
// so much computation, poor user's phone is going to melt!

There’s no getting around the first search, I guess. But is there some more efficient way to add a new thing to an array, maintaining a sort and remembering where it got placed?

  • 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-31T21:30:48+00:00Added an answer on May 31, 2026 at 9:30 pm

    This is almost certainly not an issue; NSArrays are actually hashes, and search is a lot faster than it would be for a true array. How many topics can you possibly have anyways?

    Still, if you measure the performance and find it poor, you could look into using a B-tree; Kurt Revis commented below with a link to a similar structure (a binary heap) in Core Foundation: CFBinaryHeap.

    Another option (which would also need to be measured) might be to do the comparison as you walk the array the first time; you can mark the spot and do the insertion directly:

    NSUInteger insertIndex = 0;
    NSComparisonResult prevOrder = NSOrderedDescending;
    for (Topic *topic in self.topics) {
        NSComparisonResult order = [topicId compareToTopic:topic];
        if (NSOrderedSame == order) {
            // this publication is part of an existing topic, no new index path
            [topic addPublication:pub];
            return nil;
        }
        else if( prevOrder == NSOrderedDescending && 
                 order == NSOrderedAscending )
        {
            break;
        }
        insertIndex++;
        prevOrder = order;
    }
    

    Please note that I haven’t tested this, sorry.

    I’m not sure this is actually better or faster than the way you’ve written it, though.

    Don’t worry about the work the computer is doing unless it’s demonstrably doing it too slowly.

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

Sidebar

Related Questions

I have a table of data sorted by date, from which a user can
I have a table where the results are sorted using an ORDER column, eg:
1-)For sorted array I have used Binary Search. We know that the worst case
Lets say I have a table with 20 entries. They are sorted by date
I have a sorted array of 5000 integers. How fast can I tell if
I have a sorted array of about 500,000 ints. Currently I am selecting the
I have a sorted array of points like x=[1 1 1 2 2 4
I have a sorted list at hand. Now i add a new element to
If I have an NSArray (or mutable array) with several dictionary objects (each dictionary
I have a sorted array of doubles (latitudes actually) that relatively uniformally spread out

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.