INT []数组(排序低到高) [英] int[] array (sort lowest to highest)
问题描述
所以,我不知道这是为什么变得如此对我来说很难,但我需要那种高到低,低到高。
So I am not sure why this is becoming so hard for me, but I need to sort high to low and low to high.
有关从高到低我有:
int a, b;
int temp;
int sortTheNumbers = len - 1;
for (a = 0; a < sortTheNumbers; ++a) {
for (b = 0; b < sortTheNumbers; ++b) {
if (array[b] < array[b + 1]) {
temp = array[b];
array[b] = array[b + 1];
array[b + 1] = temp;
}
}
}
不过,我不能为我的生命得到它的反向(从低到高)工作,我已经经过深思熟虑的逻辑和它总是返回0的所有值。
任何帮助AP preciated!
However, I can't for the life of me get it to work in reverse (low to high), I have thought the logic through and it always returns 0's for all the values. Any help appreciated!
大局观,我有一个JTable有4列,号码,姓名,或日期条目每一列。我需要能够进行排序那些来回。
The bigger picture is that I have a JTable with 4 columns, each column with entries of numbers, names, or dates. I need to be able to sort those back and forth.
谢谢!
推荐答案
除非你认为使用已经可用的排序功能,自动装箱是欺骗:
Unless you think using already available sort functions and autoboxing is cheating:
Integer[] arr =
{ 12, 67, 1, 34, 9, 78, 6, 31 };
Arrays.sort(arr, new Comparator<Integer>()
{
@Override
public int compare(Integer x, Integer y)
{
return x - y;
}
});
System.out.println("low to high:" + Arrays.toString(arr));
打印低到高:[1,6,9,12,31,34,67,78]
如果你需要高向低变化 X-y
到 Y-X
在比较
if you need high to low change x-y
to y-x
in the comparator
这篇关于INT []数组(排序低到高)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!