2014-09-26 57 views
0

我有一個比較的3個值進行排序:機器人,比較排序錯誤

  1. ORDER_BY_Points
  2. ORDER_BY_Gdif //目標差異
  3. ORDER_BY_Goals

首先,我從拿到陣列json字符串。我的陣列發送到StandingsSort.ORDER_BY_RULES

Arrays.sort(addressArray, StandingsSort.ORDER_BY_RULES); 

這裏是我的代碼:

static final Comparator<Standings> ORDER_BY_Points = new Comparator<Standings>() { 
    public int compare(Standings a1, Standings a2) { 
     return a1.points.compareTo(a2.points); 
    } 
}; 
static final Comparator<Standings> ORDER_BY_Gdif = new Comparator<Standings>() { 
    public int compare(Standings a1, Standings a2) { 
     return a1.Gdif.compareTo(a2.Gdif); 
    } 
}; 
static final Comparator<Standings> ORDER_BY_Goals = new Comparator<Standings>() { 
    public int compare(Standings a1, Standings a2) { 
     return a1.goalsP.compareTo(a2.goalsP); 
    } 
}; 

static final Comparator<Standings> ORDER_BY_RULES = new Comparator<Standings>() { 
    public int compare(Standings a1, Standings a2) { 
     int i = ORDER_BY_Points.compare(a1,a2); 
     if(i == 0){ 
      i = ORDER_BY_Gdif.compare(a1,a2); 
      if(i == 0){ 
       i = ORDER_BY_Goals.compare(a1,a2); 
      } 
     } 
     return i; 
    } 
}; 

class Standings { 

    String teamName; 
    String goalsP; 
    String goalsM; 
    String Gdif; 
    String points; 
    @Override 
    public String toString() { 
     return "" + teamName + "," + goalsP + ":" + goalsM + "," + Gdif + "," + points + ""; 
    } 
    public Standings(String teamName, String goalsP, 
       String goalsM, String Gdif, String points) { 
     super(); 
     this.teamName = teamName; 
     this.goalsP = goalsP; 
     this.goalsM = goalsM; 
     this.Gdif = Gdif; 
     this.points = points; 
    } 
} 

但結果也不行!這裏是結果

Name, Goals, GDif, Points 
Team,11:9,2,10 
Team,5:3,2,10 
Team,9:2,7,11 
Team,0:6,-6,2 
Team,3:9,-6,2 
Team,6:9,-3,3 
Team,8:13,-5,3 
Team,8:9,-1,5 
Team,8:11,-3,5 
Team,8:7,1,5 

爲什麼比較器排序錯誤?

+1

您使用哪種方式獲得您的結果? – 2014-09-26 19:24:24

+0

@clever_trevor Arrays.sort(addressArray,StandingsSort.ORDER_BY_RULES); – sarahsdev 2014-09-26 19:37:26

+0

請修改您的帖子。你先給出結果,然後告訴你在那之後你做了什麼。更令人困惑的是「之後我做了」。因爲看起來好像在獲得結果後這樣做。所以首先告訴你使用哪種排序,然後給出結果。 – greenapps 2014-09-27 07:16:10

回答

2

由於您將點數存儲爲String s,因此「10」位於「2」,「3」和「5」之前。如果您想按數字順序比較這些數據,則需要先將它們轉換爲int

同樣,Gdif和目標的比較爲String這可能不是你想要的。

+0

好的。但是bettere將會改變所有'字符串目標'; String goalsM; String Gdif; 絃樂點數;'在積分榜上整數。 – greenapps 2014-09-29 06:59:58