Sort NSArray and return NSArray?

I am just looking at sorting NSArrayof NSNumbersin numerical order, but I'm a little unsure of how best to go. In my way of thinking, 001 and 002 are quite comparable, so I would suspect that either he would. For 003, I'm not sure if returning NSMutableArraywhen the method expects NSArrayis good practice, it works, but it feels awkward.

-(NSArray *)testMethod:(NSArray *)arrayNumbers {    
    // 001
    NSMutableArray *sortedArray = [NSMutableArray arrayWithArray:arrayNumbers];
    [sortedArray sortUsingSelector:@selector(compare:)];
    arrayNumbers = [NSArray arrayWithArray:sortedArray];
    return(arrayNumbers);   
}

.

-(NSArray *)testMethod:(NSArray *)arrayNumbers {    
    // 002
    NSMutableArray *sortedArray = [NSMutableArray arrayWithArray:arrayNumbers];
    [sortedArray sortUsingSelector:@selector(compare:)];
    arrayNumbers = [[sortedArray copy] autorelease];
    return(arrayNumbers);   
}

.

-(NSArray *)testMethod:(NSArray *)arrayNumbers {    
    // 003
    NSMutableArray *sortedArray = [NSMutableArray arrayWithArray:arrayNumbers];
    [sortedArray sortUsingSelector:@selector(compare:)];
    return(sortedArray);    
}
+3
source share
3 answers

You do not need a mutable array. You can simply:

NSArray* sortedArray = [arrayNumbers sortedArrayUsingSelector:@selector(compare:)];
+12
source

I think you can just call

return [arrayNumbers sortedArrayUsingSelector:@selector(compare:)];
+5
source

NSArray * sortedArray = [arrayNumbers sortedArrayUsingSelector: @selector (compare:)];

0

Source: https://habr.com/ru/post/1739322/