2017-10-14 10 views
0

일부 게임 후에 순위가 포함 된 dataGrid가 있습니다. 나는 dataGrid가 우선 순위를 매기고 그 다음에는 목표 차이를 정렬하도록하고 싶습니다. XML로 여러 게임 시트에서 데이터를 수집하는 각 팀 (1-12)에 대해 다양한 변수가 있어야합니다.actionscript 3.0 여러 조건에서 데이터 격자 정렬

"WIN"에서 "GD"보다 먼저 정렬 할 수 있습니까?

감사합니다.

function Standings(){ 
var myDP5: DataProvider = new DataProvider(); 
     for (var k: int = 1; k < 13 ; k++) { 
      var TEAM_0 =this['TEAM_'+ k]; 
      var TEAM_0_GF: Array = this['TEAM_' + k+"_GF"]; 
      var TEAM_0_GP: int = this['TEAM_' + k+"_GP"]; 
      var TEAM_0_GA: int = this['TEAM_' + k+"_GA"]; 
      var TEAM_0_win: int = this['TEAM_' + k+"_win"]; 
      var TEAM_0_loss: int = this['TEAM_' + k+"_loss"]; 
      var TEAM_0_GD:int =TEAM_0_GF.length -TEAM_0_GA; 
      myDP5.addItem({ 
       "TEAM": TEAM_0, 
       "GP": TEAM_0_GP, 
       "WIN": TEAM_0_win, 
       "LOSS": TEAM_0_loss, 
       "GF": TEAM_0_GF.length, 
       "GA": TEAM_0_GA, 
       "GD": TEAM_0_GD 


      }); 

     } 

     standings_grid = new DataGrid(); 
     standings_grid.addEventListener(Event.CHANGE, velgeKamp); 
     standings_grid.columns = ["TEAM", "GP", "WIN","LOSS","GF","GA","GD"]; 
     standings_grid.dataProvider = myDP5; 
     standings_grid.rowCount = 20; 
     standings_grid.move(50, 50); 
     standings_grid.width = 500; 
     standings_grid.columns[0].width = 120; 
     standings_grid.columns[1].width = 70; 
     standings_grid.columns[2].width = 50; 
     standings_grid.columns[3].width = 50; 
     standings_grid.columns[4].width = 50; 
     standings_grid.columns[5].width = 50; 
     standings_grid.columns[6].width = 50; 

     standings_grid.width = 440; 
     standings_grid.resizableColumns = false; 
     standings_grid.sortOn("WIN",Array.DESCENDING); 
     //standings_grid.sortItemsOn("LOSS", Array.NUMERIC); 
     //oversikt_gridd.height = 500; 
     standings_grid.sortableColumns = true; 
     addChild(standings_grid); 
} 

답변

1

아주 간단합니다.

var aList:Array = new Array; 

for (var k: int = 1; k < 13 ; k++) 
{ 
    var TEAM_0 = this['TEAM_'+ k]; 
    var TEAM_0_GF: Array = this['TEAM_' + k + "_GF"]; 
    var TEAM_0_GP: int = this['TEAM_' + k + "_GP"]; 
    var TEAM_0_GA: int = this['TEAM_' + k + "_GA"]; 
    var TEAM_0_win: int = this['TEAM_' + k + "_win"]; 
    var TEAM_0_loss: int = this['TEAM_' + k + "_loss"]; 
    var TEAM_0_GD:int = TEAM_0_GF.length - TEAM_0_GA; 

    var anEntry:Object = 
    { 
     "TEAM": TEAM_0, 
     "GP": TEAM_0_GP, 
     "WIN": TEAM_0_win, 
     "LOSS": TEAM_0_loss, 
     "GF": TEAM_0_GF.length, 
     "GA": TEAM_0_GA, 
     "GD": TEAM_0_GD 
    }; 

    aList.push(anEntry); 
} 

aList.sort(compareEntries); 

// -1 to sort A before B 
// 0 to not sort A and B 
// 1 to sort A after B 
function compareEntries(A:Object, B:Object):Number 
{ 
    // I don't know what exactly you need 
    // but I believe you'll figure it out. 

    if (A['WIN'] > B['WIN']) return -1; 
    if (A['WIN'] < B['WIN']) return 1; 

    // Now if A['WIN'] == B['WIN']. 

    if (A['GD'] > B['GD']) return -1; 
    if (A['GD'] < B['GD']) return 1; 

    return 0; 
} 

// Now pass the sorted items to the DataProvider. 
var myDP5:DataProvider = new DataProvider; 
while (aList.length) myDP5.addItem(aList.shift()); 
+0

감사합니다 : 당신은 다음 .sort (...) 그것은 인수 때 compareFunction 과를 항목의배열을 만들어야합니다! @ 오르가니 스 – martinml