title: Objective C: Sort Two Dimensional Array
body: <p>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.</p>
  
  <p>Best...SL</p>
  
  <pre><code>//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&lt;[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];
      }
  }
  </code></pre>