Sorting Array in increasing order

萝らか妹 提交于 2019-11-29 23:27:02

问题


I have an array that contains values like 0,3,2,8 etc.I want to sort my array in increasing order.Please tell me how to do this.

Thanks in advance!!


回答1:


If your array is an NSArray containing NSNumbers:

NSArray *numbers = [NSArray arrayWithObjects:
                    [NSNumber numberWithInt:0],
                    [NSNumber numberWithInt:3],
                    [NSNumber numberWithInt:2],
                    [NSNumber numberWithInt:8],
                    nil];

NSSortDescriptor* sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:nil ascending:YES selector:@selector(localizedCompare:)];
NSArray *sortedNumbers = [numbers sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];

Keep in mind though, that this is just one way to sort an NSArray.

Just to name a few other methods of NSArray:

  • sortedArrayHint
  • sortedArrayUsingFunction:context:
  • sortedArrayUsingFunction:context:hint:
  • sortedArrayUsingDescriptors:
  • sortedArrayUsingSelector:
  • sortedArrayUsingComparator:
  • sortedArrayWithOptions:usingComparator:

If your array is a c int array containing ints:

#include <stdio.h>
#include <stdlib.h>
int array[] = { 0, 3, 2, 8 };
int sort(const void *x, const void *y) {
    return (*(int*)x - *(int*)y);
}
void main() {
    qsort(array, 10, sizeof(int), sort);
}



回答2:


You don't need a sort descriptor if your array is that simple with NSNumbers only. If it's called array, then

[array sortedArrayUsingSelector:@selector(compare:)];

will return a new array sorted in increasing order, as you want.




回答3:


NSSortDescriptor *sortDescriptor;
sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:nil
                                              ascending:YES] autorelease];
NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
NSArray *sortedArray;
sortedArray = [**YourArray** sortedArrayUsingDescriptors:sortDescriptors];



回答4:


Sorting has many solutions, each with different efficiencies.

The easiest and most natural sort, in my opinion, is insertion sort. Go to this page and scroll down to look at the code. Insertion sort

For a more complete list of all of the sorting algorithms, check this page




回答5:


Use the code: [[myData allKeys]sortedArrayUsingSelector:@selector(psuedoNumericCompare:)];

myData is your array. If you use that line in your code, it will sort on size of the integer (and not alphabetic, so it won't sort like 1, 111, 2,322, 333, 4445, 45, 67 but like 1, 2, 45, 67, 111, 322, 333, 4445).

Original source: How to let the sortedArrayUsingSelector using integer to sort instead of String?




回答6:


You can sort the array in simple method using sortUsingSelector the code as follows.

[array sortUsingSelector:@selector(compare:options:)];
NSLog(@"array after sort in max:%@",array);

i think its the simplest way without using any descriptors.




回答7:


Try this .If you have Array intArr like {1,10.11,12,2,23}

NSArray *array = [intArr copy];
    NSArray *sortedArray = [array sortedArrayUsingComparator: ^(id obj1, id obj2) {

        if ([obj1 integerValue] == 0 && [obj2 integerValue] == 0)
        {
            return (NSComparisonResult)NSOrderedSame;
        }
        if ([obj1 integerValue] == 0)
        {
            return (NSComparisonResult)NSOrderedDescending;
        }
        if ([obj2 integerValue] == 0)
        {
            return (NSComparisonResult)NSOrderedAscending;
        }

        if ([obj1 integerValue] > [obj2 integerValue])
        {
            return (NSComparisonResult)NSOrderedDescending;
        }

        if ([obj1 integerValue] < [obj2 integerValue])
        {
            return (NSComparisonResult)NSOrderedAscending;
        }
        return (NSComparisonResult)NSOrderedSame;
    }];


来源:https://stackoverflow.com/questions/5649850/sorting-array-in-increasing-order

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!