Javascript: How to sort an array of records by values in one of the fields?
本问题已经有最佳答案,请猛点这里访问。
Possible Duplicate:
Sorting objects in an array by a field value in JavaScript
假设我有一组记录:
谢谢
直截了当的方法
1 2 3 4 5 | var sorted = [{a:0,b:0},{a:2,b:1},{a:1,b:2}].sort( function( a, b ) { if ( a.a == b.a ) return 0; return ( a.a > b.a ) ? 1 : -1; }).reverse(); |
编辑
更灵活的方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | // Note: console.log() require Firebug var records = [{a:0,b:0},{a:2,b:1},{a:1,b:2}]; console.log( records ); // Sorty by 'a' ascending sortByProperty( records, 'a' ); console.log( records ); // Sort by 'b' descending sortByProperty( records, 'b', true ); console.log( records ); function sortByProperty( arr, property, descending ) { arr.sort( function( a, b ) { return Boolean( descending ) ? b[property] - a[property] : a[property] - b[property] } ); } |
编辑2
也适用于字符串的版本
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | // Note: console.log() require Firebug var records = [ {a:0,b:0} , {a:2,b:1} , {a:'banana',b:'apple'} , {a:1,b:2} , {a:'apple',b:'banana'} ]; console.log( records ); // Sorty by 'a' ascending sortByProperty( records, 'a' ); console.log( records ); // Sort by 'b' descending sortByProperty( records, 'b', true ); console.log( records ); function sortByProperty( arr, property, descending ) { arr.sort( function( a, b ) { var c = a[property].toString() , d = b[property].toString() if ( c == d ) return 0; return Boolean( descending ) ? d > c ? 1 : -1 : d < c ? 1 : -1 } ); } |
休斯,我的同事今天给我看了以下内容。
注意-cmp()和cmp()用于降序和升序。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | 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}]; // a ascending arr.sort(function(x, y){ return cmp(x.a, y.a) < cmp(y.a, x.a) ? -1:1; }); // a descending arr.sort(function(x, y){ return -cmp(x.a, y.a) < -cmp(y.a, x.a) ? -1:1; }); // a ascending, b descending arr.sort(function(x, y){ return [cmp(x.a, y.a), -cmp(x.b, y.b)] < [cmp(y.a, x.a), -cmp(y.b,x.b)] ? -1:1; }); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | // your items array var items = [{a:0,b:0},{a:2,b:1},{a:1,b:2}]; // function we can use as a sort callback var compareItemsBy_a_Descending = function(x,y) { return y.a - x.a; }; // function to alert the items array var displayItems = function(items) { var out = []; for (var i=0;i<items.length;i++) { out.push('{a:' + items[i].a + ',b:' + items[i].b + '}'); } alert('[' +out.join(',') + ']'); }; // run it displayItems(items); |
结果:A:0,B:0,A:2,B:1,A:1,B:2]
1 2 3 4 5 | // sort it items.sort(compareItemsBy_a_Descending); // run it again displayItems(items); |
结果:A:2,B:1,A:1,B:2,A:0,B:0]
排序委托怎么样?
1 2 3 4 5 | [{a:0,b:0},{a:2,b:1},{a:1,b:2}].sort(function(a,b){ // see http://www.javascriptkit.com/javatutors/arraysort.shtml // for an explanation of this next line return b.a-a.a; }); |
(存钱之后,我注意到另外两个几乎完全相同的答案,但我会把我的答案留在这里,因为它们之间的细微差别。)
使用闭包比直接引用函数慢。
1 2 3 4 5 | // assume var records = [{a:0,b:0},{a:2,b:1},{a:1,b:2}]; records.sort(myCustomSort); function myCustomSort(a, b) { return (b.a - a.a); } |
如果新数组确实需要第二个变量,只需在调用自定义排序方法之前复制初始数组。