如何在javascript中排序数组?

use*_*729 5 javascript arrays sorting

var arr = [];
arr.push(row1);
arr.push(row2);
...
arr.push(rown);
Run Code Online (Sandbox Code Playgroud)

怎么排序row['key']

Tim*_*own 11

JavaScript数组有一个内置sort()方法.在这种情况下,类似下面的内容将起作用:

arr.sort( function(row1, row2) {
    var k1 = row1["key"], k2 = row2["key"];
    return (k1 > k2) ? 1 : ( (k2 > k1) ? -1 : 0 );
} );
Run Code Online (Sandbox Code Playgroud)