简体   繁体   English

如何在java中按列(升序)对2D数组进行排序

[英]How to sort 2D array according to column wise(ascending order) in java

I want to sort the array according to column wise(ascending order).The method sortByrollNum works correctly.But in second method sortByPer the highest number print on the top.It should be print according ascending order as the first method do.I tried a lot.Can anybody help me how to fix it? 我想按列顺序(升序)对数组进行排序。方法sortByrollNum正常工作。但是在第二种方法sortByPer中,最高数字打印在顶部。它应该按升序打印,作为第一种方法。我尝试了很多人可以帮我解决这个问题吗?

public static int compareStrings(String str1,String str2){
    int check=str1.compareTo(str2);
    return check;
}//RollNumCheck

public static void sortByRollNum(String [] [] arr,int currentSize){
    String minName=arr[0][0];
    String minRollNum=arr[0][1];
    String minPer=arr[0][2];
    for(int j=0;j<arr.length-1;j++){
        for(int i=0;i<arr.length-j-1;i++){
            int check=compareStrings(arr[i][1], arr[i+1][1]);
            if(check>0){
                minName=arr[i+1][0];
                minRollNum=arr[i+1][1];
                minPer=arr[i+1][2];
                arr[i+1][0]=arr[i][0];
                arr[i+1][1]=arr[i][1];
                arr[i+1][2]=arr[i][2];
                arr[i][0]=minName;
                arr[i][1]=minRollNum;
                arr[i][2]=minPer;
            }//if
        }//for
    }//for
    System.out.print("\nSorted Array according to Roll Number.\n");
    printArray(arr);
}//sortByRollNum

public static void sortByPer(String [] [] arr,int currentSize){
    String minName=arr[0][0];
    String minRollNum=arr[0][1];
    String minPer=arr[0][2];
    for(int j=0;j<arr.length-1;j++){
        for(int i=0;i<arr.length-j-1;i++){
            int check=compareStrings(arr[i][2], arr[i+1][2]);
            if(check>0){
                minName=arr[i+1][0];
                minRollNum=arr[i+1][1];
                minPer=arr[i+1][2];
                arr[i+1][0]=arr[i][0];
                arr[i+1][1]=arr[i][1];
                arr[i+1][2]=arr[i][2];
                arr[i][0]=minName;
                arr[i][1]=minRollNum;
                arr[i][2]=minPer;
            }//if
        }//for
    }//for
    System.out.print("\nSorted Array according to Percentage.\n");
    printArray(arr);
}//sortByPer

It seems like the problem is in your implementation of the compareString(String str1,String str2) method. 似乎问题出在compareString(String str1,String str2)方法的实现中。 When you use the String.compareTo(String) function, it compares the two strings by converting them to Unicode values to compare. 当您使用String.compareTo(String)函数时,它会通过将两个字符串转换为要比较的Unicode值来比较这两个字符串。 Even the sortByRollNum() function would not work in all cases, it was just lucky that it did. 甚至sortByRollNum()函数在所有情况下都不起作用,它确实很幸运。

My suggestion would be since you're comparing the Roll Numbers and Percentages, parse out the integer values and compare those. 我的建议是,因为您要比较滚动数和百分比,解析整数值并进行比较。 You can use Integer.parseInt(String) to do so but the string must contain only numeric values. 您可以使用Integer.parseInt(String)执行此操作,但字符串必须仅包含数值。

About compareTo: https://beginnersbook.com/2013/12/java-string-compareto-method-example/ 关于compareTo: https//beginnersbook.com/2013/12/java-string-compareto-method-example/

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

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