I've written some method for sorting asc and desc numbers in array. All I want to know is this good logic, and is there a space for improving this code?-(NSArray *)sortHanded:(NSArray *)inputArray ascending:(BOOL)ascending {    NSMutableArray *inputMutableArray = [inputArray mutableCopy];    NSMutableArray *sortedArray = [NSMutableArray new];    NSNumber *min;    NSNumber *max;    if (ascending == YES) {        //starting while        while (inputMutableArray.count > 0) {            //starting switch            switch (inputMutableArray.count) {                    //case 0                case 0:                    break;                    //case 1                case 1:                    [sortedArray addObject:[inputMutableArray objectAtIndex:0]];                    [inputMutableArray removeObject:[inputMutableArray objectAtIndex:0]];                    break;                    //starting default switch                default:                    for (NSNumber *temp in inputMutableArray) {                        if (min == nil) {                            min = temp;                        }                        if (min > temp) {                            min = temp;                        }                    }                    [sortedArray addObject:min];                    [inputMutableArray removeObject:min];                    min = nil;                    break;                    //end default                    //end start switch            }            //ending while        }    }    else {        //starting while        while (inputMutableArray.count > 0) {            //starting switch            switch (inputMutableArray.count) {                    //case 0                case 0:                    break;                    //case 1                case 1:                    [sortedArray addObject:[inputMutableArray objectAtIndex:0]];                    [inputMutableArray removeObject:[inputMutableArray objectAtIndex:0]];                    break;                    //starting default switch                default:                    for (NSNumber *temp in inputMutableArray) {                        if (max == nil) {                            max = temp;                        }                        if (max < temp) {                            max = temp;                        }                    }                    [sortedArray addObject:max];                    [inputMutableArray removeObject:max];                    max = nil;                    break;                    //end default                    //end start switch            }            //ending while        }    }        //returning sorted array    return sortedArray;}
My own method for sorting numbers in array
array;sorting;objective c
There is a big flaw in your program: Thisif (min > temp)compares the addresses of the number objects, not their values.The correct way isif ([min compare:temp] == NSOrderedDescending)// ...if ([max compare:temp] == NSOrderedAscending)You probably didn't notice the problem because small numbers are represented as tagged pointers, but withNSArray *array = @[@40000000000000, @300000, @10000000000000, @16];your code gave a wrong result in my test.Resetting min after the loop seems a bit counter-intuitive to me.I would do that at the beginning, this also allows to restrict the scope of that variable to where it is needed. Also the second if should bean else-if:NSNumber *min = nil;for (NSNumber *temp in inputMutableArray) {    if (min == nil) {        min = temp;    } else if ([min compare:temp] == NSOrderedDescending) {        min = temp;    }}Of course you could contract the if statement to    if (min == nil || [min compare:temp] == NSOrderedDescending) {        min = temp;    }that is a matter of taste.You could get rid of this loop by using Key-Value Coding to determine the minimumvalue in the array:NSNumber *min = [inputMutableArray valueForKeyPath:@@min.self];But note that this can be significantly slower for large arrays(compare Finding the smallest and biggest value in NSArray of NSNumbers).Having almost identical code for the two cases (ascending/descending sort) is not necessary. If you defineNSComparisonResult sortOrder = ascending ? NSOrderedAscending : NSOrderedDescending;at the beginning, then both cases can be treated withwhile (inputMutableArray.count > 0) {    NSNumber *first = nil;    for (NSNumber *temp in inputMutableArray) {        if (first == nil || [temp compare:first] == sortOrder) {            first = temp;        }    }    [sortedArray addObject:first];    [inputMutableArray removeObject:first];}