Sort a 2D array by the second value

前端 未结 2 948
难免孤独
难免孤独 2021-01-01 04:47

I have an array and I want to sort by the number field not the name.

var showIt = [
  [\"nuCycleDate\",19561100],
  [\"ndCycleDate\",19460700],
  [\"neCycleD         


        
2条回答
  •  灰色年华
    2021-01-01 05:27

    This site advises against using the arguments without assigning to temporary variables. Try this instead:

    showIt.sort(function(a, b) {
        var x = a[1];
        var y = b[1];
        return x - y;
    });
    

提交回复
热议问题