0

I am new in iOS.And I am doing like this

NSMutableArray* arr1 = [NSMutableArray arrayWithObjects: @"A", @"C", @"E", nil];
NSMutableArray* arr2 = [NSMutableArray arrayWithObjects: @"B", @"D", @"F", nil];
NSMutableArray* animals = [NSMutableArray arrayWithArray:arr1];
[animals addObjectsFromArray: arr2];

It give me output like ACEBDE

But I need output like ABCDEF

Any Hint

Muju
  • 884
  • 20
  • 54

4 Answers4

0

I tried now.I got it.Check the below answer.You have to use sortDescriptor simply.

NSMutableArray* arr1 = [NSMutableArray arrayWithObjects: @"A", @"C", @"E", nil];
NSMutableArray* arr2 = [NSMutableArray arrayWithObjects: @"B", @"D", @"F", nil];
NSMutableArray* animals = [NSMutableArray arrayWithArray:arr1];
[animals addObjectsFromArray: arr2];

NSArray *sortedArray = [animals sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
NSLog(@"The sortedArrays are - %@",sortedArray);

The printed results are

The sortedArrays are - (
A,
B,
C,
D,
E,
F
)
user3182143
  • 9,459
  • 3
  • 32
  • 39
0

Just loop them

NSMutableArray* arr1 = [NSMutableArray arrayWithObjects: @"A", @"C", @"E", nil];
NSMutableArray* arr2 = [NSMutableArray arrayWithObjects: @"B", @"D", @"F", nil];
NSMutableArray* animals = [NSMutableArray new];

NSUInteger maxCount = arr1.count > arr2.count ? arr1.count : arr2.count;
for (int i = 0; i < maxCount; i ++) {
    if ([arr1 objectAtIndex:i]) {
        [animals addObject:[arr1 objectAtIndex:i]];
    }
    if ([arr2 objectAtIndex:i]) {
        [animals addObject:[arr2 objectAtIndex:i]]
    }
}

This will work for all types of arrays.

Pavel Gatilov
  • 2,570
  • 2
  • 18
  • 31
  • Can you please answer my question http://stackoverflow.com/questions/43539307/how-to-post-string-with-special-character-and-thai-language-using-xml-parsing-in – Muju Apr 22 '17 at 06:44
0

try this -

NSMutableArray* arr1 = [NSMutableArray arrayWithObjects: @"A", @"C", @"E", nil];
    NSMutableArray* arr2 = [NSMutableArray arrayWithObjects: @"B", @"D", @"F", nil];
    NSUInteger maxCount = arr1.count > arr2.count ? arr1.count : arr2.count;
    NSMutableArray* animals = [[NSMutableArray alloc] init];

    for (int i=0; i<maxCount; i++)
    {
        if (i<arr1.count) {
            [animals addObject:[arr1 objectAtIndex:i]];
        }
        if (i<arr2.count) {
            [animals addObject:[arr2 objectAtIndex:i]];
        }

    }
Anupam Mishra
  • 3,408
  • 5
  • 35
  • 63
0

Adding another answer just for fun.

NSMutableArray* arr1 = [NSMutableArray arrayWithObjects: @"A", @"C", @"E", nil];
NSMutableArray* arr2 = [NSMutableArray arrayWithObjects: @"B", @"D", @"F", nil];
NSMutableArray* animals = [NSMutableArray new];

NSInteger minCount;
NSMutableArray *maxArray;

if (arr1.count < arr2.count) {
    minCount = arr1.count;
    maxArray = arr2;
}
else {
    minCount = arr2.count;
    maxArray = arr1;
}

for (int i = 0; i < minCount; i++) {
    [animals addObject:arr1[i]];
    [animals addObject:arr2[i]];
}

NSInteger pendingItemsLength;
if ((pendingItemsLength = maxArray.count - minCount)) {
    [animals addObjectsFromArray:[maxArray subarrayWithRange:NSMakeRange(minCount, pendingItemsLength)]];
}

This is efficient than the accepted answer as it avoids if condition inside a for loop.

Adithya
  • 4,545
  • 3
  • 25
  • 28