说明:property是属性名,desc是一个bool值,desc为true是升序排列 ,desc为false是降序排列
使用时:var arr=[{'level':'3'},{'level':'5'},{'level':'4'},{'level':'8'},{'level':'13'}];
arr.sort( this.compare( 'level', true ) ); //升序排列
arr.sort( this.compare( 'level', false ) ); //降序排列
compare: function( property, desc ) {
return function ( a, b ) {
var value1 = a[ property ];
var value2 = b[ property ];
if ( desc == true ) {
//排序升序排列
return value1 - value2;
} else {
return value2 - value1;
}
}
},
网友评论