0

Hopefully someone can help.

I'm adding multiple objects to a NSMutableArray and I need to sort the order based on the first element which will always be a number.

However I'm unsure how to do this?

For example:

NSMutableArray *array = [[NSMutableArray alloc] init];
NSArray *object = [NSArray arrayWithObjects: @"1",@"Test",@"Test"];
[array addObject:object];

Thanks

Strikeforce
  • 361
  • 4
  • 15
  • 1
    Can you show what you've tried so far, and what isn't working? – Aaron Brager Dec 12 '13 at 17:21
  • are you always adding arrays in your mutableArray ? – shinyuX Dec 12 '13 at 17:23
  • 2
    BTW - the first value is not a number, it is a string. – rmaddy Dec 12 '13 at 17:24
  • Really hacky solution :) Why just not put sorting outside array itself – Grzegorz Krukowski Dec 12 '13 at 17:27
  • Read the spec! There are a bunch of "sortedArrayUsing.." methods on NSArray, and "sortUsing..." methods on NSMutableArray. (And this is at least the 3rd time this question has been asked this morning.) – Hot Licks Dec 12 '13 at 17:44
  • 1
    This is not a duplicate of the above mentioned post. This is a separate problem because he's driving the sort based on the first value of the array itself. IMO this is a bad design, but you could pop that first item out (sortCriterion = [array objectAtIndex:0]; [array removeObjectAtIndex:0]; then switch on your sortCriterion to select a sorting method, and inside THAT method, follow the instructions in the other post. – Rayfleck Dec 12 '13 at 19:01

2 Answers2

1

If your array always contains other arrays, and the first element of the innermost array is always a string containing a number, you could use the NSMutableArray method sortUsingComparator to sort your array:

[array sortUsingComparator: ^(NSArray* obj1, NSArray* obj2)
{
  int value1 =  [obj1[0] integerValue];
  int value2 =  [obj2[0] integerValue];
  if (value1==value2)
    return NSOrderedSame;
  else if (value1 < value2)
    return NSOrderedAscending;
  else
    return NSOrderedDescending;
} 
];  

In the sortUsingComparator family of methods, you supply a block of code that the sort method uses to compare pairs of objects in your array. The block uses the standard typedef NSComparator, which takes 2 objects as parameters and returns a value of type NSComparisonResult.

The code above will probably crash if all the objects in your array are not arrays of strings. (Actually it would work if the first element of each component array was an NSNumber, since NSNumber also responds to the integerValue message.)

If you are going to use this code in a very controlled environment where you can be sure that the data you are sorting is well-formed, it should work as written. If there is any chance that the objects in the array would be of a different type, or be empty, or that their first element would not respond to the integerValue messages, then you should add error checking code.

Duncan C
  • 128,072
  • 22
  • 173
  • 272
0

If you sort your array alphanumerically, the object @"1" will appear before any words. Keep in mind though that @"1" in your code above is a string, not a number.

As to how to sort an array, look into [NSArray sortedArrayUsingComparator:] and similar methods.

TotoroTotoro
  • 17,524
  • 4
  • 45
  • 76