如何在javascript中排序数组?

时间:2021-09-27 00:54:27
var arr = [];
arr.push(row1);
arr.push(row2);
...
arr.push(rown);

How to sort by row['key']?

如何按行排序['key']?

4 个解决方案

#1


11  

A JavaScript array has a built-in sort() method. In this case, something like the following would work:

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

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

#2


4  

You call the sort function of an array with your comparator. A JavaScript comparator is just a function that returns -1, 0, or 1 depending on whether a is less than b, a is equal to b, or a is greater than b:

用比较器调用数组的sort函数。 JavaScript比较器只是一个返回-1,0或1的函数,具体取决于a是小于b,a是等于b还是a大于b:

myarray.sort(function(a,b){
    if(a < b){
        return -1;
    } else if(a == b){
        return 0;
    } else { // a > b
        return 1;
    }
});

This is just an example, your function can base the comparison on whatever you want, but it needs to return -1,0,1.

这只是一个例子,你的函数可以根据你的需要进行比较,但它需要返回-1,0,1。

Hope this helps.

希望这可以帮助。

#3


2  

Here is set of functions if you want to sort asending, descending, or sort on multiple columns in an array.

如果要对数组中的多个列进行asending,descending或sort排序,下面是一组函数。

var cmp = function(x, y){ return x > y? 1 : x < y ? -1 : 0; },
    arr =  [{a:0,b:0},{a:2,b:1},{a:1,b:2},{a:2, b:2}];

// sort on column a ascending
arr.sort(function(x, y){
    return cmp( cmp(x.a, y.a), cmp(y.a, x.a) );
});

// sort on column a descending
arr.sort(function(x, y){
    return cmp( -cmp(x.a, y.a), -cmp(y.a, x.a) );
});

// sort on columns a ascending and b descending
arr.sort(function(x, y){
    return cmp([cmp(x.a, y.a), -cmp(x.b, y.b)], [cmp(y.a, x.a), -cmp(y.b,x.b)]);
});

To get an ascending sort, use "cmp(...)", and to get a descending sort, use "-cmp(...)"

要获得升序排序,请使用“cmp(...)”,并获得降序排序,请使用“-cmp(...)”

And to sort on multiple columns, compare two arrays of cmp(...)

并对多列进行排序,比较两个cmp数组(...)

#4


0  

Consider the following code:

请考虑以下代码:

var arr = new Array();

for(var i = 0; i < 10; ++i) {
    var nestedArray = [ "test", Math.random() ];
    arr.push(nestedArray);
}

function sortBySecondField(a, b) {
    var aRandom = a[1];
    var bRandom = b[1];

    return ((aRandom < bRandom) ? -1 : ((aRandom > bRandom) ? 1 : 0));
}

arr.sort(sortBySecondField);

alert(arr);

Now just change a sortBySecondField function to compare a['key'] instead of a[1] and do the same for b.

现在只需更改sortBySecondField函数来比较['key']而不是[1],并对b执行相同操作。

#1


11  

A JavaScript array has a built-in sort() method. In this case, something like the following would work:

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

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

#2


4  

You call the sort function of an array with your comparator. A JavaScript comparator is just a function that returns -1, 0, or 1 depending on whether a is less than b, a is equal to b, or a is greater than b:

用比较器调用数组的sort函数。 JavaScript比较器只是一个返回-1,0或1的函数,具体取决于a是小于b,a是等于b还是a大于b:

myarray.sort(function(a,b){
    if(a < b){
        return -1;
    } else if(a == b){
        return 0;
    } else { // a > b
        return 1;
    }
});

This is just an example, your function can base the comparison on whatever you want, but it needs to return -1,0,1.

这只是一个例子,你的函数可以根据你的需要进行比较,但它需要返回-1,0,1。

Hope this helps.

希望这可以帮助。

#3


2  

Here is set of functions if you want to sort asending, descending, or sort on multiple columns in an array.

如果要对数组中的多个列进行asending,descending或sort排序,下面是一组函数。

var cmp = function(x, y){ return x > y? 1 : x < y ? -1 : 0; },
    arr =  [{a:0,b:0},{a:2,b:1},{a:1,b:2},{a:2, b:2}];

// sort on column a ascending
arr.sort(function(x, y){
    return cmp( cmp(x.a, y.a), cmp(y.a, x.a) );
});

// sort on column a descending
arr.sort(function(x, y){
    return cmp( -cmp(x.a, y.a), -cmp(y.a, x.a) );
});

// sort on columns a ascending and b descending
arr.sort(function(x, y){
    return cmp([cmp(x.a, y.a), -cmp(x.b, y.b)], [cmp(y.a, x.a), -cmp(y.b,x.b)]);
});

To get an ascending sort, use "cmp(...)", and to get a descending sort, use "-cmp(...)"

要获得升序排序,请使用“cmp(...)”,并获得降序排序,请使用“-cmp(...)”

And to sort on multiple columns, compare two arrays of cmp(...)

并对多列进行排序,比较两个cmp数组(...)

#4


0  

Consider the following code:

请考虑以下代码:

var arr = new Array();

for(var i = 0; i < 10; ++i) {
    var nestedArray = [ "test", Math.random() ];
    arr.push(nestedArray);
}

function sortBySecondField(a, b) {
    var aRandom = a[1];
    var bRandom = b[1];

    return ((aRandom < bRandom) ? -1 : ((aRandom > bRandom) ? 1 : 0));
}

arr.sort(sortBySecondField);

alert(arr);

Now just change a sortBySecondField function to compare a['key'] instead of a[1] and do the same for b.

现在只需更改sortBySecondField函数来比较['key']而不是[1],并对b执行相同操作。