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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T22:19:31+00:00 2026-06-03T22:19:31+00:00

sortedArray contains 100+ objects that need to be organize by the ‘hour’ in the

  • 0

sortedArray contains 100+ objects that need to be organize by the ‘hour’ in the ‘time’ key. The outcome is to have an array of arrays to populate the sections of a table and use the hour for the index. ‘time’ is of the form – 2012-05-15 07:20:00

NSInteger d = 0;
NSInteger prevd;
NSMutableArray *tempArray = [[NSMutableArray alloc] init];
detailArray = [[NSMutableArray alloc] init];
for (NSInteger i = 0; i < 24; ++i)
    [detailArray addObject:[NSNull null]];

for (NSArray *item in sortedArray) {
    prevd = d;
    d = [[[[item objectAtIndex:0] objectForKey:@"time"] substringWithRange:NSMakeRange(11, 2)] intValue];
    if (d > prevd && prevd != 0) {
        [detailArray replaceObjectAtIndex:d withObject:tempArray];
        [tempArray removeAllObjects];
    }
    [tempArray addObject:item];
}

The objects are not ending up in the correct detailArray postition or not ending up in it at all. For instance, [detailArray objectAtIndex:6] give items with time 14 (2pm).

What am I missing?

Edit to correct code with 1st answer. It is now:

NSInteger d = 0;
NSInteger prevd;
NSMutableArray *tempArray = nil;
detailArray = [[NSMutableArray alloc] init];
for (NSInteger i = 0; i < 24; ++i)
    [detailArray addObject:[NSNull null]];

for (NSArray *item in sortedArray) {
    prevd = d;
    d = [[[[item objectAtIndex:0] objectForKey:@"time"] substringWithRange:NSMakeRange(11, 2)] intValue];
    if (d > prevd) {
        tempArray = NSMutableArray array];
        [detailArray replaceObjectAtIndex:d withObject:tempArray];
    }
    [tempArray addObject:item];
}
  • 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-03T22:19:32+00:00Added an answer on June 3, 2026 at 10:19 pm

    You can’t reuse tempArray like that, you’re going to end up with detailArray containing multiple entries that all point to the same tempArray instance. (i.e. every slot that had a time is going to end up with the array for the last time slot).

    Instead of

    [detailArray replaceObjectAtIndex:d withObject:tempArray];
    [tempArray removeAllObjects];
    

    you’ll want

    tempArray = [NSMutableArray array];
    [detailArray replaceObjectAtIndex:d withObject:tempArray];
    

    that way each time slot has its own array.
    And you’ll also want to change your original declaration of tempArray to be

    NSMutableArray *tempArray = nil;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an NSArray of UILocalNotification objects that I need to sort according to
hi i have an array of objects which need to be sorted (alphabet on
1-)For sorted array I have used Binary Search. We know that the worst case
I have a sorted array of doubles (latitudes actually) that relatively uniformally spread out
I have a array of NSString objects which I have to sort by descending.
I have to sort this array in descening order with the key likecount. How
I have an sorted array which contains first names of people. This array has
Suppose I have a sorted array of integers int[] , and I want to
I'm trying to sort a simple array which only contains a few decimal numbers.
I have a view that displays in a UIPopoverController. Before, it just had properties

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.