Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.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_Arrays_Sorting - Fatal编程技术网

Java 按字母顺序正确排序数组时出现问题

Java 按字母顺序正确排序数组时出现问题,java,arrays,sorting,Java,Arrays,Sorting,我的部分代码有问题。它必须按名称按字母顺序排序,但似乎是按降序排序的。有人能告诉我应该做哪些更改,以便输出正确显示数据吗?多谢各位 这是我的分类代码: //Method sortName public static void sortName() throws IOException { //Selection Sort for (x = 0; x < 8; x++) { smalle

我的部分代码有问题。它必须按名称按字母顺序排序,但似乎是按降序排序的。有人能告诉我应该做哪些更改,以便输出正确显示数据吗?多谢各位

这是我的分类代码:

//Method sortName
            public static void sortName() throws IOException {

            //Selection Sort
            for (x = 0; x < 8; x++) {
                smallest = x;

            for(i = x + 1; i < 8; i++) {

                //Compare current smallest
                //to the current position in the array
                if(name[i].compareTo(name[smallest])> 0) {
                    smallest = i;
                }

            }
                //Swap smallest element with position in array      
                temp = name [x];
                name [x] = name [smallest];
                name [smallest] = temp;

                temp = crime [x];
                crime [x] = crime [smallest];
                crime [smallest] = temp;

                temp = year [x];
                year [x] = year [smallest];
                year [smallest] = temp;

            }
            //Display each category of records; names, crime, year
            System.out.print("Name" + " ----" + "Crime" + "----" + "Year\n");

            //output
            for (x = 0; x < 8; x++) {

                //Display each sorted criminal name with the crime and year. 
                System.out.println(name[x] + " --- " + crime[x] + " --- " + year[x]);
看一下比较器。它说:

返回: 负整数、零或正整数,因为此对象小于、等于或大于指定对象

你行吗

if(name[i].compareTo(name[smallest])> 0) {
该对象中提到的名称为[i],指定的对象为名称[minimate]。当name[i]小于name[minimate]时,您希望进行交换。因此,要么交换名称[i]和名称[minimate],要么将比较改为<0


此外,我始终建议在第一次测试调试程序时,使用调试程序逐步执行代码,尤其是在出现意外行为时。

将此行更改为:name[I].compareToname[minimate]>0改为name[I].compareToname[minimate]>因此,您已经成功地按降序进行排序,并且希望改为升序。只要把条件颠倒一下?可能是重复的谢谢!我的程序现在正按照我想要的方式工作!
if(name[i].compareTo(name[smallest])> 0) {