Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我该如何反转排序?_Java_Sorting - Fatal编程技术网

Java 我该如何反转排序?

Java 我该如何反转排序?,java,sorting,Java,Sorting,到目前为止,我编写了这个程序,它将学生的分数从最低到最高进行排序。我需要在从最高到最低排序的地方反转它 public static void sortStudentScores(String namesArry[], int scoresArry[]) { int tempScore; String tempName; for (int i = 0; i < scoresArry.length; i++) { for ( int j= i + 1

到目前为止,我编写了这个程序,它将学生的分数从最低到最高进行排序。我需要在从最高到最低排序的地方反转它

public static void sortStudentScores(String namesArry[], int scoresArry[]) {
    int tempScore;
    String tempName;

    for (int i = 0; i < scoresArry.length; i++) {

        for ( int j= i + 1; j < scoresArry.length; j++) {

            if (scoresArry[i] > scoresArry[j]) {
                tempScore = scoresArry[i];
                scoresArry[i] = scoresArry[j];
                scoresArry[j] = tempScore;
                tempName = namesArry[i];
                namesArry[i] = namesArry[j];
                namesArry[j] = tempName;

            }
        }
    }

    System.out.println("\nAscending Order:");

    for (int i = 0; i < namesArry.length; i ++) {
        System.out.println("Student Name: " + namesArry[i] + " " + scoresArry[i]);
    }
}   
publicstaticvoidsortstudentscores(字符串namesArry[],int scoresArry[]){
积分;
字符串tempName;
for(int i=0;iscoresary[j]){
tempScore=scoresArry[i];
记分制[i]=记分制[j];
记分制[j]=临时记分制;
tempName=namesArry[i];
namesArry[i]=namesArry[j];
namesArry[j]=临时名称;
}
}
}
System.out.println(“\n加密顺序:”);
for(int i=0;i
按如下方式更新您的代码:

if (scoresArry[i] < scoresArry[j]) 
if(scoresary[i]
我建议您定义一个班级学生,并定义排序的比较器(请参阅:)

公共静态void sortStudentScores(字符串namesArry[],int scoresArry[]){
积分;
字符串tempName;
for(int i=0;i
这里只是修改了排序逻辑,在代码中,您确保索引越小的元素得分越低。要按降序进行,只需做相反的事情


干杯

尝试更改此条件。。如果(scoresary[i]>scoresary[j])将
更改为
,只需在排序后反转数组即可
public static void sortStudentScores(String namesArry[], int scoresArry[]) {
    int tempScore;
    String tempName;

    for (int i = 0; i < scoresArry.length; i++) {

        for ( int j= i + 1; j < scoresArry.length; j++) {

            if (scoresArry[i] < scoresArry[j]) {
                tempScore = scoresArry[i];
                scoresArry[i] = scoresArry[j];
                scoresArry[j] = tempScore;
                tempName = namesArry[i];
                namesArry[i] = namesArry[j];
                namesArry[j] = tempName;

            }
        }
    }

    System.out.println("\nDescending Order:");

    for (int i = 0; i < namesArry.length; i ++) {
        System.out.println("Student Name: " + namesArry[i] + " " + 
scoresArry[i]);
    }
}