创建一个从最低到最高对数组进行排序的方法

时间:2015-08-28 18:32:22

标签: java arrays sorting

我想创建一个函数来对二维数组进行排序,从最低到最高得分,但如果得分为0,我不想对它进行排序。

我希望我的数组看起来像这样:

enter image description here

我的阵列:

private int[][] scores = new int[5][2];

方法:

public void sortByScore(){


    scores[0][0] = 0;
    scores[1][0] = 2;
    scores[2][0] = 4;
    scores[3][0] = 6;
    scores[0][1] = 233;
    scores[1][1] = 123;
    scores[2][1] = 542;
    scores[3][1] = 231;

    for(int i=0;i<scores.length-1;i++){

        if(scores[i][0]>scores[i+1][0]){

            int temp =scores[i][0];
            scores[i+1][0]=temp;

            scores[i][0]=scores[i+1][0];


            printArray();
        }


    }

}

4 个答案:

答案 0 :(得分:1)

我的印象是您正在尝试将PID值与数字分数相关联,并且您希望对此数据进行排序,以便您首先获得最高分。如果这是你的目标,那么我认为二维数组是一个糟糕的数据结构选择。作为替代方案,您可以创建一个自定义类型(类),它将PID和分数配对,为您提供一个紧凑的对象,如下所示:

public class Score implements Comparable<Score> {
    private int pid;
    private int score;

    public Score(int pid, int score) {
        this.pid = pid;
        this.score = score;
    }

    public int compareTo(Score other) {
        return Integer.compare(this.score, other.score);
    }
}

现在您可以为每个PID创建一个Score对象:

Score alpha = new Score(1, 134);
Score beta = new Score(2, 156);
Score gamma = new Score(3, 121);

然后将它们添加到NavigableSet中,这将根据他们的&#34;自然顺序排序&#34;它由compareTo类的Score方法定义:

NavigableSet<Score> scores = new TreeSet<>();
scores.add(alpha);
scores.add(beta);
scores.add(gamma);

然后你可以按顺序获得得分,最高得分,创建一个降序视图(从最高值开始),然后像这样迭代:

NavigableSet<Score> highestScoresFirst = scores.descendingSet();
for (Score score : highestScoresFirst) {
    // Do something with the current score, such as print it out.
}

还有其他方法可以做到这一点,你可以用数组来做。但我认为您目前使用的结构会有问题。

答案 1 :(得分:0)

使用冒泡排序算法可以很好地解决这类问题:

public static void BubbleSort(int[] num) {
    int j;
    boolean flag = true; // set flag to true to begin first pass
    int temp; // holding variable
    while (flag) {
        flag = false; // set flag to false awaiting a possible swap
        for (j = 0; j < num.length - 1; j++) {
            if (num[j] > num[j + 1]) // change to > for ascending sort
            {
                temp = num[j]; // swap elements
                num[j] = num[j + 1];
                num[j + 1] = temp;
                flag = true; // shows a swap occurred
            }
        }
    }
}

这只是一个例子,你必须让它适应你的要求......

有关this link

的更多信息

答案 2 :(得分:0)

使用Comparator

java.util.Arrays.sort(array, new java.util.Comparator<Integer[]>() {
    public int compare(int[] a, int[] b) {
        return Integer.compare(a[0], b[0]);
    }
});

答案 3 :(得分:0)

由于您的数组仅支持对scores[i][0]部分进行排序,因此其余部分(例如scores[i][1])仍未排序。 由于您的数组是一个二维数组,因此需要2 for loops才能成功排序整个2D数组。因此排序代码应如下所示......

//This for loop will move through every part of you 2D Array
int k = 0; //[k][] could be of any from the present array as it just used for the length of sub-array
 for(int i=0;i<scores[k].length-1;i++){ //Length of Sub-array

    for(int j=0;i<scores.length-1;i++) { //Length of Array

        if(scores[j][i]>scores[j+1][i]){

            int temp =scores[j][i];
            scores[j+1][i]=temp;
            scores[j][i]=scores[j+1][i];

            printArray();
        }
    }
}