按升序排序数组 [英] Sorting Array in increasing order
本文介绍了按升序排序数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个包含0,3,2,8等值的数组。我想按递增顺序对数组进行排序。请告诉我如何执行此操作。
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.
提前致谢!!
推荐答案
如果您的数组是包含NSNumbers的NSArray:
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]];
请记住,这只是对NSArray进行排序的一种方法。
Keep in mind though, that this is just one way to sort an NSArray.
仅举几个NSArray的其他方法:
Just to name a few other methods of NSArray:
- sortedArrayHint
- sortedArrayUsingFunction:context:
- sortedArrayUsingFunction:context:hint:
- sortedArrayUsingDescriptors:
- sortedArrayUsingSelector:
- sortedArrayUsingComparator:
- sortedArrayWithOptions:usingComparator:
- sortedArrayHint
- sortedArrayUsingFunction:context:
- sortedArrayUsingFunction:context:hint:
- sortedArrayUsingDescriptors:
- sortedArrayUsingSelector:
- sortedArrayUsingComparator:
- sortedArrayWithOptions:usingComparator:
如果您的阵列是交流包含整数的int数组:
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);
}
这篇关于按升序排序数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文