2017-05-25 118 views
2

我有卡陣列,我有按鈕的排序,但我不知道如何做鑽石,俱樂部,鍬,心臟卡想要分開從該卡..如何排序卡,如鑽石,俱樂部,鏟子,動作心臟3.0

var aList:Array = 
      [ 
       {card:Globe.self.realstage.joker_mc, x:605.55, y:195.45}, 
       {card:Globe.self.realstage.king_mc, x:323.80, y:298.45}, 
       {card:Globe.self.realstage.queen_mc, x:45.85, y:213.95}, 
       {card:Globe.self.realstage.a_mc,  x:605.55, y:195.45}, 
       {card:Globe.self.realstage.ten_mc, x:323.80, y:298.45}, 
       {card:Globe.self.realstage.five_mc, x:45.85, y:213.95}, 
       {card:Globe.self.realstage.two_mc, x:605.55, y:195.45}, 
       {card:Globe.self.realstage.nine_mc, x:323.80, y:298.45}, 
       {card:Globe.self.realstage.four_mc, x:45.85, y:213.95}, 
      ]; 

任何人都知道,你可以請詳細說明這個one.Thank你

回答

3

我建議增加一些額外的參數,說「重」:

var aList:Array = 
     [ 
      {card:Globe.self.realstage.joker_mc, x:605.55, y:195.45, weight: 11}, 
      {card:Globe.self.realstage.king_mc, x:323.80, y:298.45, weight: 13}, 
      {card:Globe.self.realstage.queen_mc, x:45.85, y:213.95, weight: 12}, 
      {card:Globe.self.realstage.a_mc,  x:605.55, y:195.45, weight: 14}, 
      {card:Globe.self.realstage.ten_mc, x:323.80, y:298.45, weight: 10}, 
      {card:Globe.self.realstage.five_mc, x:45.85, y:213.95, weight: 5}, 
      {card:Globe.self.realstage.two_mc, x:605.55, y:195.45, weight: 2}, 
      {card:Globe.self.realstage.nine_mc, x:323.80, y:298.45, weight: 9}, 
      {card:Globe.self.realstage.four_mc, x:45.85, y:213.95, weight: 4}, 
     ]; 

,然後排序陣列基於這個重量:

// in descending order 
aList.sort(function (c1:Object, c2:Object):int 
     { 
      if (c1.weight > c2.weight) return -1; 
      if (c1.weight < c2.weight) return 1; 
      return 0; 
     }); 

// in ascending order: 
aList.sort(function (c1:Object, c2:Object):int 
     { 
      if (c1.weight > c2.weight) return 1; 
      if (c1.weight < c2.weight) return -1; 
      return 0; 
     }); 

如果你不能改變的對象(或由於某種原因你不想增加體重有),可以創建一個外部輔助功能:

// somewhere 
function getWeight(data: Object):int { 
    switch(data.card) { 
     case Globe.self.realstage.two_mc: 
      return 2; 
     case Globe.self.realstage.four_mc: 
      return 4; 

     ... 

     default: return 0; 
    } 
} 

aList.sort(function (c1:Object, c2:Object):int 
    { 
     if (getWeight(c1) > getWeight(c2)) return 1; 
     if (getWeight(c1) < getWeight(c2)) return -1; 
     return 0; 
    });