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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T05:02:13+00:00 2026-06-16T05:02:13+00:00

I have an array of arrays. The contained array’s first elements are all NSDate

  • 0

I have an array of arrays. The contained array’s first elements are all NSDate objects. I would like to sort the array containing the arrays in order from most recent to least. For some reason, the below sorting algorithm results in an infinite loop. Can anyone help me out? Thank you.

Best…SL

//array is the array containing all of the other arrays(that have NSDates as their first elements)
//temp is the new array being added to the end of the array, to later be sorted into the correct position.

[array addObject:temp];    
NSMutableArray *tempArray;

for (int i=0; i<[array count]; i++) 
{
    NSDate *session1, *session2;
    session1 = [[array objectAtIndex:i] objectAtIndex:0];
    session2 = [[array objectAtIndex:[array count]-1] objectAtIndex:0];

    if([session1 compare:session2] == NSOrderedDescending)
{
        tempArray = [array objectAtIndex:i];
        [array insertObject:[array objectAtIndex:[array count]-1] atIndex:i];
        [array insertObject:tempArray atIndex:[array count]-1];
    }
}
  • 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-16T05:02:15+00:00Added an answer on June 16, 2026 at 5:02 am

    This results in an infinite loop because, in every step, you’re inserting two more values into the array. Thus your array is growing faster than you are traversing it. I’m assuming you meant to swap the values.

    In any case, a much simpler and more efficient sort is to use the built-in sorting capabilities:

    // NSArray *sortedArray, with the unsorted 'array' pulled from some other instance
    sortedArray = [array sortedArrayUsingComparator:^(id a, id b) {
        return [[b objectAtIndex:0] compare:[a objectAtIndex:0]];
    }];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a map containers.Map(array1, array2) that I would like to sort according to
I have an array of dictionaries. I would like to filter that array by
I have two arrays with different attributes for the objects contained in each. participants
I have an array of Password objects, each containing a tag , a username
I have an array with nested arrays that looks like this: var tw =
I have data contained in an array which is like so, $file['info']['files'] = array(
I have an array of objects called objarray . Each object looks like this:
My problem: I have an JSON Array containing arrays, which i render into the
I have an array that contains an array of arrays if that makes any
I have an array ($configOptions) which contains some arrays. Each array ($option) in that

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.