0
I am sorting a array of string numbers using ios inbuilt sorting method but it is giving me wrong output.So I applied bubble sorting for a while,Any body can explaing why it is behaving like that.So that I can optimize my code.

NSArray *numbers=@[@"45",@"2",@"11",@"31",@"240",@"310"];
numbers=[numbers sortedArrayUsingSelector:@selector(compare:)];

NSLog(@"sorted array is %@",numbers);

NSMutableArray *m_Array=[[NSMutableArray alloc] initWithArray:numbers];

[numbers enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
    for (int j=idx+1; j<numbers.count; j++) {

        if ([m_Array[idx] intValue]>[m_Array[j] intValue]) {
            NSString *temp=m_Array[idx];
            [m_Array replaceObjectAtIndex:idx withObject:m_Array[j]];
            [m_Array replaceObjectAtIndex:j withObject:temp];
        }
    }
}];

NSLog(@"sorted array after bubble sort is %@",m_Array);

output is

sorted array is ( 11, 2, 240, 31, 310, 45 )

sorted array after bubble sort is ( 2, 11, 31, 45, 240, 310 )

2

3 Answers 3

3

That's because you are comparing string objects, not numbers.

Try changing your array to be numbers and not strings (which are in quotes).

In other words, instead of

NSArray *numbers=@[@"45",@"2",@"11",@"31",@"240",@"310"];

you do:

NSArray *numbers=@{@45,@2,@11,@31,@240,@310};

(which are Objective-C literals, as described in this documentation), you'll see much better results.

The reason the "bubble sort" method is working better for you is because you get the "intValue" of your string objects in that array. That's not happening for the first algorithm.

Sign up to request clarification or add additional context in comments.

1 Comment

I accepted your answer because it is the optimized way to declare and use integer array.Thanks
1

Use NSNumber rather than using string for adding integer values to an array.

 NSMutableArray *array =[NSMutableArray alloc]initWithObjects:[NSNumber      numberWithInteger:12],[[NSNumber numberWithInteger:122] ];

And then sort

[array sortedArrayUsingSelector:@selector(compare:)]

Comments

0

This is because sorting in Objective-C sorts data with by first element and if the first element is same then it looks for the next one otherwise it sorts on the basis of first element value.suppose case of 11 and 2 , as it checks for first element and first element of 2 is greater than first element of 11 (i.e; 1).So it will declare 2 as greater for sorting purpose.And 2 will come after 11.

For sorting you have to keep prefix values of numbers in order to sort properly.For example: 001,002,003 for 3 digits no and 01,02,03 for two digit no.

NSMutableArray *tempArray=[[NSMutableArray alloc] initWithCapacity:[numbers count]];
[numbers enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
    [tempArray addObject:[NSString stringWithFormat:@"%03d",[numbers[idx] intValue]]];
}];

NSLog(@"sorted array is %@",[tempArray sortedArrayUsingSelector:@selector(compare:)]);

Note:---Only for variable digit size,--- count maximum no in array and count its digits programmically and set string format accordingly.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.