我想在JavaScript中使用两个属性布尔值和Int值对对象数组进行排序。我希望像这样输出:
{
first_name: 'sumeet',
last_name: 'Prentice',
offerApplicable: 'TRUE',
price: 40
}, {
first_name: 'Pirate',
last_name: 'Prentice',
offerApplicable: 'TRUE',
price: 50
}, {
first_name: 'Lazslo',
last_name: 'Jamf',
offerApplicable: 'TRUE',
price: 60
}, {
first_name: 'jitendra',
last_name: 'Prentice',
offerApplicable: 'TRUE',
price: 101
}
具有真值的所有对象首先也按价格排序,然后所有具有布尔值的对象为false。我能够按布尔值对它们进行排序,但我也希望按价格对它们进行排序。
我试过这个
var objs = [
{ first_name: 'Lazslo', last_name: 'Jamf' ,offerApplicable: 'TRUE' ,price: 60 },
{ first_name: 'Pig', last_name: 'Bodine' , offerApplicable: 'FALSE' ,price: 100},
{ first_name: 'Pirate', last_name: 'Prentice' ,offerApplicable: 'TRUE' ,price: 50},
{ first_name: 'nithesh', last_name: 'Bodine' , offerApplicable: 'FALSE' ,price: 40},
{ first_name: 'sumeet', last_name: 'Prentice' ,offerApplicable: 'TRUE' ,price: 40},
{ first_name: 'mahesh', last_name: 'Bodine' , offerApplicable: 'FALSE' ,price: 40},
{ first_name: 'jitendra', last_name: 'Prentice' ,offerApplicable: 'TRUE' ,price: 101}
];
function compare(a,b) {
var aConcat = a["offerApplicable"] + a["price"];
var bConcat = b["offerApplicable"] + b["price"];
if (aConcat < bConcat )
return 1;
if (aConcat > bConcat )
return -1;
return 0;
}
console.log(objs.sort(compare));
我得到像这样的输出
first_name:“Lazslo”last_name:“Jamf”offerApplicable:“TRUE” 价格:60
:first_name:“Pirate”last_name:“Prentice”offerApplicable:“TRUE” 价格:50
first_name:“sumeet”last_name:“Prentice”offerApplicable:“TRUE” 价格:40
first_name:“jitendra”last_name:“Prentice”offerApplicable: “真实”价格:101
first_name:“nithesh”last_name:“Bodine”offerApplicable:“FALSE” 价格:40
first_name:“mahesh”last_name:“Bodine”offerApplicable:“FALSE” 价格:40
first_name:“Pig”last_name:“Bodine”offerApplicable:“FALSE” 价格:100
提前任何帮助。
答案 0 :(得分:0)
只需再做几步即可。
首先,获取不同数组中的所有真假组件。
像这样const trueEntities = defaultArray.filter(e => e.offerApplicable === 'TRUE');
const falseEntities = defaultArray.filter(e => e.offerApplicable === 'FALSE');
然后按价格对此数组进行排序
const sortedByPriceTrue = trueEntities.sort((a, b) => a.price - b.price);
const sortedByPriceFalse = falseEntities.sort((a, b) => a.price - b.price);
然后简单地在变量数组中返回/使用
return sortedByPriceTrue.concat(sortedbyPriceFalse);
我知道这个解决方案可能比一次操作慢,但更具可读性
P.S。编辑,第一个解决方案也没有工作
答案 1 :(得分:0)
objs.sort(function (a, b) {
return (a.price - b.price);
});
objs.sort(function (a, b) {
var price1 = (a.offerApplicable === 'TRUE');
var price2 = (b.offerApplicable === 'TRUE');
return price2 - price1;
});
首先,我们按价格和价格对价值进行排序。然后,按offerApplicable字段对值进行排序。
综合解决方案:
objs.sort(function (a, b) {
var price1 = (a.offerApplicable === 'TRUE');
var price2 = (b.offerApplicable === 'TRUE');
return price2 - price1 || a.price - b.price;
});
答案 2 :(得分:0)
您可以链接Array#sort
的排序函数的rewturn值的条件。
第一部分
(b.offerApplicable === 'TRUE') - (a.offerApplicable === 'TRUE')
检查offerApplicable
的值是否为'TRUE'
。与true
的比较会将项目移至底部,因此我们需要使用a
和b
切换支票。
第二部分
a.price - b.price
仅在先前的delta为零时进行评估,然后返回价格的delta。
var array = [{ first_name: 'Lazslo', last_name: 'Jamf', offerApplicable: 'TRUE', price: 60 }, { first_name: 'Pig', last_name: 'Bodine', offerApplicable: 'FALSE', price: 100 }, { first_name: 'Pirate', last_name: 'Prentice', offerApplicable: 'TRUE', price: 50 }, { first_name: 'nithesh', last_name: 'Bodine', offerApplicable: 'FALSE', price: 40 }, { first_name: 'sumeet', last_name: 'Prentice', offerApplicable: 'TRUE', price: 40 }, { first_name: 'mahesh', last_name: 'Bodine', offerApplicable: 'FALSE', price: 40 }, { first_name: 'jitendra', last_name: 'Prentice', offerApplicable: 'TRUE', price: 101 }];
array.sort(function (a, b) {
return (b.offerApplicable === 'TRUE') - (a.offerApplicable === 'TRUE') || a.price - b.price;
});
console.log(result);
&#13;
.as-console-wrapper { max-height: 100% !important; top: 0; }
&#13;