2017-05-19 77 views
-1

我有一個這樣的數組。 asd = [[a,3],[b,1],[c,5],[d,2]];如何在Apps腳本中對二維數組進行排序

我想將它從所得像的數量進行排序,

ASD = [[一,5],[B,3],[C,2],[d,1]];

任何人都知道應用程序腳本代碼?

+0

這聽起來像一個JavaScript題。無論如何,有幾個問題:數組是否總是以[字母,數字]的形式出現?你想要按字母順序排列字母,並且數字遞減,即使這意味着要修改數組?您是否正在尋找專門用於Google應用程序腳本的解決方案,或者是否會影響電子表格中的單元格?請考慮回答這些問題,以便我們能夠準確地幫助您。 – Adelin

+0

[爲什麼「有人可以幫助我?」不是一個真正的問題?](http://meta.stackoverflow.com/q/284236) –

回答

1

下面的樣本如何? a,b,c和d被用作字符串。這可以在Google腳本編輯器上執行。

示例腳本:

var asd = [['a',3],['b',1],['c',5],['d',2]]; 

var result = [[asd[i][0],[i[1] for each (i in asd)].sort(
    function(a,b){ 
     if(a > b) return -1; 
     if(a < b) return 1; 
     return 0; 
})[i]] for (i in asd)]; 

結果:

[[a, 5.0], [b, 3.0], [c, 2.0], [d, 1.0]] 
0

假設你想達到什麼樣的,這個代碼應工作:

var arr = [["a",3],["b",1],["c",5],["d",2]]; 

for (var i = 0; i < arr.length; i++){ 
for (var j = i+1; j < arr.length; j++) { 
    if (i==arr.length-1) continue; 
    var thisItem = arr[i], 
     nextItem = arr[j]; 
    if (thisItem[0] > nextItem[0]) { 
    var tempL = nextItem[0]; 
    nextItem[0] = thisItem[0]; 
    thisItem[0] = tempL; 
    } 

    if (thisItem[1] < nextItem[1]) { 
    var tempN = nextItem[1]; 
    nextItem[1] = thisItem[1]; 
    thisItem[1] = temp; 
    } 
} 
} 
相關問題