简体   繁体   English

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

[英]Create a method that sorts an array from lowest to highest

I'm wanting to create a function to sort a two dimensional array, by the score from lowest to highest, but if the score is 0 I don't want to sort it .我想创建一个函数来按从最低到最高的分数对二维数组进行排序,但如果分数为 0,我不想对其进行排序。

I want my array to look like this:我希望我的数组看起来像这样:

在此处输入图片说明

my array:我的阵列:

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

the method:方法:

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();
        }


    }

}

I get the impression that you're trying to link a PID value to a numerical score and that you'd like to sort this data so that you get the highest score first.我的印象是,您试图将 PID 值与数字分数联系起来,并且您想对这些数据进行排序,以便首先获得最高分。 If this is your aim then I think a two-dimensional array is a poor choice of data structure.如果这是您的目标,那么我认为二维数组是一种糟糕的数据结构选择。 As an alternative you could create a custom type (class) which pairs the PID and the score to give you a compact object, like this:作为替代方案,您可以创建一个自定义类型(类),将 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);
    }
}

Now you can create a Score object for each PID like this:现在您可以为每个 PID 创建一个Score对象,如下所示:

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

Then add them into a NavigableSet which will order them based on their "natural ordering" which is defined by the compareTo method of the Score class:然后将它们添加到NavigableSet ,它将根据Score类的compareTo方法定义的“自然顺序”对它们进行排序:

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

Then you can get the scores in order, highest score first, by creating a descending view (starting with highest value first) and then iterating through it like this:然后你可以通过创建一个降序视图(首先从最高值开始)然后像这样迭代它来按顺序获得分数,最高分在前:

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

There are other ways of doing this, and you could do it with arrays.还有其他方法可以做到这一点,你可以用数组来做到这一点。 But I think that the structure you're currently using will be problematic.但我认为你目前使用的结构会有问题。

This kind of problems are good solved using bubble sort algorithm:使用冒泡排序算法可以很好地解决此类问题:

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 is only an example, you have to adapt it to your requeriments...这只是一个例子,你必须适应你的要求......

More information on this link有关此链接的更多信息

Using a 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]);
    }
});

Since your array only supports the sorting of the scores[i][0] part, the rest parts such as scores[i][1] remains unsorted.由于您的数组仅支持对scores[i][0]部分进行排序,因此scores[i][1]等其余部分保持未排序。 As your array is a 2-Dimensional Array, you need 2 for loops for successfully sorting the whole 2D array.由于您的数组是二维数组,因此您需要 2 个for loops才能成功对整个二维数组进行排序。 So the sorting code should be as below...所以排序代码应该如下...

//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();
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM