2015-08-28 99 views
-1

我想創建一個函數來按照從低到高的分數對二維數組進行排序,但如果得分爲0我不想排序它。創建一個從最低到最高排序數組的方法

我希望我的陣列看起來像這樣:

enter image description here

我的數組:

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

方法:

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


    } 

} 
+4

不能幫助沒有代碼 – silentprogrammer

+0

你到目前爲止有什麼? – toskv

+0

排序前您的初始數組是什麼,舉例 – krzydyn

回答

1

更多信息,我得到你想要一個PID值鏈接到的印象一個數字分數,並且您想要對這些數據進行排序,以便您首先獲得最高分。如果這是您的目標,那麼我認爲二維數組是數據結構的糟糕選擇。作爲替代,你可以創建一個自定義類型(類)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); 
    } 
} 

現在你可以爲每個PID這樣創建一個Score對象:

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

然後將它們添加到NavigableSet將根據他們的「自然順序」,這是由compareTo方法Score類的定義命令他們:

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

這時你可以先拿到分數才能,最高分,通過創建一個下降的觀點(從最高值第一),然後通過它迭代這樣的:

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

有這樣做的其他方式,你可以用數組來完成它。但我認爲你目前使用的結構會有問題。

0

這樣那樣的問題都很好解決使用冒泡排序算法:

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 
      } 
     } 
    } 
} 

這只是一個例子,你要它適應你requeriments ......上this link

0

使用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]); 
    } 
}); 
0

由於您的陣列只支持scores[i][0]部分的分選,其餘零件如scores[i][1]仍然未排序。 由於您的陣列是二維陣列,因此您需要2 for loops才能成功排序整個二維陣列。所以排序代碼應該如下...

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