Sort array in ascending order

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

Thanks in advance!

+4
source share
7 answers

If your NSArray contains 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 that this is just one way to sort NSArray.

Just to name a few other NSArray methods:


If your array is an 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); } 
+14
source

You don't need a sort handle if your array is just with NSNumber . If it is called array , then

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

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

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

Sorting has many solutions, each of which has a different efficiency.

The simplest and most natural look, in my opinion, is insertion sorting. Go to this page and scroll down to look at the code. Sort Insert

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

+1
source

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

myData is your array. If you use this line in your code, it will sort by the size of the integer (and not alphabetically, so it will not look like 1, 111, 2,322, 333, 4445, 45, 67 , but like 1, 2, 45, 67, 111, 322, 333, 4445 ).

Source: How to enable ArrayUsingSelector sorting using integer to sort instead of string?

+1
source

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

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

I think this is the easiest way without using descriptors.

0
source

Try it. If you have an Array intArr, for example {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; }]; 
0
source

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


All Articles