我有一个当前按名称排序的函数和一个值/键对数组。
我想知道如何传递正在执行哪种键的键,这样我每次都可以调用相同的函数:
var arr = [{name:'bob', artist:'rudy'},
{name:'johhny', artist:'drusko'},
{name:'tiff', artist:'needell'},
{name:'top', artist:'gear'}];
sort(arr, 'name'); //trying to sort by name
sort(arr, 'artist'); //trying to sort by artist
function sort(arr) {
arr.sort(function(a, b) {
var nameA=a.name.toLowerCase(), nameB=b.name.toLowerCase();
if (nameA < nameB) //sort string ascending
return -1;
if (nameA > nameB)
return 1;
return 0; //default return value (no sorting)
});
}
答案 0 :(得分:41)
Array.prototype.sortOn = function(key){
this.sort(function(a, b){
if(a[key] < b[key]){
return -1;
}else if(a[key] > b[key]){
return 1;
}
return 0;
});
}
var arr = [{name:'bob', artist:'rudy'},{name:'johhny', artist:'drusko'},{name:'tiff', artist:'needell'},{name:'top', artist:'gear'}];
arr.sortOn("name");
arr.sortOn("artist");
答案 1 :(得分:19)
以下是两个可能有用的排序函数:
// sort on values
function srt(desc) {
return function(a,b){
return desc ? ~~(a < b) : ~~(a > b);
};
}
// sort on key values
function keysrt(key,desc) {
return function(a,b){
return desc ? ~~(a[key] < b[key]) : ~~(a[key] > b[key]);
}
}
对于您的阵列,您可以使用以下方式对“名称”进行排序:
var arr = [ {name:'bob', artist:'rudy'}
,{name:'johhny', artist:'drusko'}
,{name:'tiff', artist:'needell'}
,{name:'top', artist:'gear'}]
.sort(keysrt('name'));
您还可以组合排序功能:
function srt(desc,key) {
return function(a,b){
return desc ? ~~(key ? a[key]<b[key] : a < b)
: ~~(key ? a[key] > b[key] : a > b);
};
}
并使用
var arr = [ {name:'bob', artist:'rudy'}
,{name:'johhny', artist:'drusko'}
,{name:'tiff', artist:'needell'}
,{name:'top', artist:'gear'}]
.sort(srt(null,'name'));
这是一种一体化解决方案:
function srt(on,descending) {
on = on && on.constructor === Object ? on : {};
return function(a,b){
if (on.string || on.key) {
a = on.key ? a[on.key] : a;
a = on.string ? String(a).toLowerCase() : a;
b = on.key ? b[on.key] : b;
b = on.string ? String(b).toLowerCase() : b;
// if key is not present, move to the end
if (on.key && (!b || !a)) {
return !a && !b ? 1 : !a ? 1 : -1;
}
}
return descending ? ~~(on.string ? b.localeCompare(a) : a < b)
: ~~(on.string ? a.localeCompare(b) : a > b);
};
}
// usage examples
'a,z,x,y,a,b,B,Z,a,i,j,y'.split(',').sort( srt({string:true;}) );
//=> ,a,a,b,B,i,j,x,y,y,z,Z
[100,7,8,2,2,0,5,1,6,5,-1].sort( srt() );
//=> -1,0,1,2,2,5,5,6,7,8,100
[100,7,8,2,2,0,5,1,6,5,-1].sort( srt({},true}) );
//=> 100,8,7,6,5,5,2,2,1,0,-1
var objarr =
[ {name:'bob', artist:'rudy'}
,{name:'Johhny', artist:'drusko'}
,{name:'Tiff', artist:'needell'}
,{name:'top', artist:'gear'}]
.sort( srt({key:'name',string:true}, true) );
for (var i=0;i<objarr.length;i+=1) {
console.log(objarr[i].name);
}
//=> logs zeb, top, Tiff, Johnny consecutively
答案 2 :(得分:6)
function keysrt(key) {
return function(a,b){
if (a[key] > b[key]) return 1;
if (a[key] < b[key]) return -1;
return 0;
}
}
someArrayOfObjects.sort(keysrt('text'));
答案 3 :(得分:2)
让你的生活变得轻松,并使用一个封闭 https://stackoverflow.com/a/31846142/1001405
您可以看到工作示例here
var filter = 'name', //sort by name
data = [{name:'bob', artist:'rudy'},{name:'johhny', artist:'drusko'},{name:'tiff', artist:'needell'},{name:'top', artist:'gear'}];;
var compare = function (filter) {
return function (a,b) { //closure
var a = a[filter],
b = b[filter];
if (a < b) {
return -1;
}else if (a > b) {
return 1;
} else {
return 0;
}
};
};
filter = compare(filter); //set filter
console.log(data.sort(filter));
答案 4 :(得分:0)
看看所有答案,我提出了自己的跨浏览器解决方案。接受的解决方案在IE或Safari中不起作用。此外,其他解决方案不允许通过降序进行排序。
/*! FUNCTION: ARRAY.KEYSORT(); **/
Array.prototype.keySort = function(key, desc){
this.sort(function(a, b) {
var result = desc ? (a[key] < b[key]) : (a[key] > b[key]);
return result ? 1 : -1;
});
return this;
}
var arr = [{name:'bob', artist:'rudy'}, {name:'johhny', artist:'drusko'}, {name:'tiff', artist:'needell'}, {name:'top', artist:'gear'}];
arr.keySort('artist');
arr.keySort('artist', true);