按第二列结果排序二维数组

时间:2013-12-02 00:40:54

标签: arrays sorting multidimensional-array

创建了我自己的排序算法,但它不起作用。有什么建议或更简单的方法吗?花了整整一天......这太疯狂了!

2Darray的样本

2342 88 98

3901 75 71

1444 60 85

5327 95 80

4888 83 100

需要按第二列排序,因此结果如下所示

5327 95 80

2342 88 98

4888 83 100

3901 75 71

1444 60 85

for(int row = 0; row < examArray.length-1; row++){

        //this checks the second column only. I left is static under column since we only compare that column. for loop will change rows
        if(tempArray[row][1]<tempArray[row+1][1]){
            //if first final(2nd column) is LESS than 2nd final then I need to swap 1st with second using for loop. Confused here!

            for(int i = 0; i < 1; i++){
                for(int j = 0; j < examArray[row].length; j++){

                    //this loads NEW array with new Highest studend and copys the entire row to the first location and so on...
                    sortedExamArray [i][j] = examArray[row+1][j];
                    tempArray[i][j] = examArray[i][j];
                    sortedExamArray[row+1][j]=tempArray[i][j];
                    tempArray[row+1][j]= examArray[i][j];
              tempArray[i][j] = sortedExamArray[i][j];

0 个答案:

没有答案