根据列对二维int数组进行排序的过程

时间:2022-06-25 20:25:27

i will show you an example about the purpose of the question .The array i have before and how we want it after the sorting :

我将向您展示一个关于问题目的的例子。我之前拥有的数组以及排序后我们想要的数据:

Before :

Box    Weight    Priority
1       50          5
2       30          8
3       90          6
4       20          7  
5       80          9

After :

Box    Weight    Priority
3       90          6
5       80          9
1       50          5
2       30          8
4       20          7

we work in the int matrix :

我们在int矩阵中工作:

data= new int[BoxNumber][3];

The sorting is based in the second column Weight.Am looking for a procedure that sort the data array.

排序基于第二列Weight.Am寻找对数据数组进行排序的过程。

 public void sortC(int[][] temp)
{
    if (temp.length >= 2)
    {
        for (int i = 1; i <= temp.length - 1; i++)
        {
            int[] hold = temp[i];
            int[] holdP = temp[i-1];

            int j = i;

            while (j > 0 && hold[1] < holdP[1]) // 1 represents the reference of sorting
            {
                hold = temp[j];
                holdP = temp[j-1];

                temp[j] = holdP;
                temp[j-1] = hold;

                j--;
            }
        }
    }
}

 sortC(data);

I tried this one, but unfortunately is doesn't give a right sorting i couldn't figure out the pickle. Some help plz ?

我试过这个,但不幸的是没有给出正确的排序我无法弄清楚泡菜。一些帮助PLZ?

2 个解决方案

#1


6  

Use java.util.Arrays.sort with a custom Comparator.

将java.util.Arrays.sort与自定义Comparator一起使用。

int[][] temp = { { 1, 50, 5 }, { 2, 30, 8 }, { 3, 90, 6 },
        { 4, 20, 7 }, { 5, 80, 9 }, };
Arrays.sort(temp, new Comparator<int[]>() {
    @Override
    public int compare(int[] o1, int[] o2) {
        return Integer.compare(o2[1], o1[1]);
    }
});

As shmosel mentioned below, with Java 8, you can use:

正如下面提到的shmosel,使用Java 8,您可以使用:

Arrays.sort(temp, Comparator.comparingInt(arr -> arr[1]));

#2


1  

You can do this instead of writing your own sorting algorithm:

您可以这样做,而不是编写自己的排序算法:

int[][] n = new int[10][];
//init your array here

List<int[]> ints = Arrays.asList(n);
Collections.sort(ints, new Comparator<int[]>() {
    @Override
    public int compare(int[] o1, int[] o2) {
        return o1[1] - o2[1]; // compare via second column
    }
});

and if you want make it array again:

如果你想再次使它成为数组:

int[][] result = ints.toArray(n);

#1


6  

Use java.util.Arrays.sort with a custom Comparator.

将java.util.Arrays.sort与自定义Comparator一起使用。

int[][] temp = { { 1, 50, 5 }, { 2, 30, 8 }, { 3, 90, 6 },
        { 4, 20, 7 }, { 5, 80, 9 }, };
Arrays.sort(temp, new Comparator<int[]>() {
    @Override
    public int compare(int[] o1, int[] o2) {
        return Integer.compare(o2[1], o1[1]);
    }
});

As shmosel mentioned below, with Java 8, you can use:

正如下面提到的shmosel,使用Java 8,您可以使用:

Arrays.sort(temp, Comparator.comparingInt(arr -> arr[1]));

#2


1  

You can do this instead of writing your own sorting algorithm:

您可以这样做,而不是编写自己的排序算法:

int[][] n = new int[10][];
//init your array here

List<int[]> ints = Arrays.asList(n);
Collections.sort(ints, new Comparator<int[]>() {
    @Override
    public int compare(int[] o1, int[] o2) {
        return o1[1] - o2[1]; // compare via second column
    }
});

and if you want make it array again:

如果你想再次使它成为数组:

int[][] result = ints.toArray(n);