简体   繁体   English

我想在JavaScript中使用两个属性布尔值和Int对对象数组进行排序

[英]I want to sort an array of objects in JavaScript with Two properties Boolean value and Int

I want to sort an array of objects in JavaScript with Two properties Boolean value and Int value.I want Output like this : 我想在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
}

All object with true values are first also sorted with price and then all object having boolean value as false. 具有真值的所有对象首先也按价格排序,然后所有具有布尔值的对象为false。 i am able to sort them by boolean value but i want to sort them by price as well. 我能够按布尔值对它们进行排序,但我也希望按价格对它们进行排序。

i have tried this 我试过这个

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));

i am getting output like this 我得到这样的输出

first_name : "Lazslo" last_name : "Jamf" offerApplicable : "TRUE" price : 60 first_name:“Lazslo”last_name:“Jamf”offerApplicable:“TRUE”价格:60

:first_name : "Pirate" last_name : "Prentice" offerApplicable : "TRUE" price : 50 :first_name:“Pirate”last_name:“Prentice”offerApplicable:“TRUE”价格:50

first_name : "sumeet" last_name : "Prentice" offerApplicable : "TRUE" price : 40 first_name:“sumeet”last_name:“Prentice”offerApplicable:“TRUE”价格:40

first_name : "jitendra" last_name : "Prentice" offerApplicable : "TRUE" price : 101 first_name:“jitendra”last_name:“Prentice”offerApplicable:“TRUE”价格:101

first_name : "nithesh" last_name : "Bodine" offerApplicable : "FALSE" price : 40 first_name:“nithesh”last_name:“Bodine”offerApplicable:“FALSE”价格:40

first_name : "mahesh" last_name : "Bodine" offerApplicable : "FALSE" price : 40 first_name:“mahesh”last_name:“Bodine”offerApplicable:“FALSE”价格:40

first_name : "Pig" last_name : "Bodine" offerApplicable : "FALSE" price : 100 first_name:“Pig”last_name:“Bodine”offerApplicable:“FALSE”价格:100

any help thanx in advance. 任何帮助thanx提前。

Just do it in little more steps. 只需多做几步即可。

First, get all true and false components in different arrays. 首先,获取不同数组中的所有true和false组件。

smth like this 像这样

const trueEntities = defaultArray.filter(e => e.offerApplicable === 'TRUE');
const falseEntities = defaultArray.filter(e => e.offerApplicable === 'FALSE');

then sort this arrays by price 然后按价格排序这个数组

const sortedByPriceTrue = trueEntities.sort((a, b) => a.price - b.price);
const sortedByPriceFalse =  falseEntities.sort((a, b) => a.price - b.price);

and then simply return/use in variable concated arrays 然后简单地返回/使用变量并行数组

return sortedByPriceTrue.concat(sortedbyPriceFalse);

i know this solution maybe slower then one operation, but much more readable 我知道这个解决方案可能比一个操作慢,但更具可读性

ps edited, first solution didn't work as well ps编辑,第一个解决方案也没有用

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;
});

First, we sort the values by price & then, sort the value by offerApplicable field. 首先,我们按价格对值进行排序,然后按offerApplicable字段对值进行排序。

Combined Solution: 综合解决方案

objs.sort(function (a, b) {
    var price1 = (a.offerApplicable === 'TRUE');
    var price2 = (b.offerApplicable === 'TRUE');
    return price2 - price1 || a.price - b.price;
});

You could chain the criteria for the rewturn value of the sort function of Array#sort . 您可以链接Array#sort的sort函数的rewturn值的条件。

The first part 第一部分

(b.offerApplicable === 'TRUE') - (a.offerApplicable === 'TRUE')

performes a check if the value of offerApplicable is 'TRUE' . 如果offerApplicable值为'TRUE'则执行检查。 A comparison with true moves the item to the bottom, therfore we need to switch the check with a and b . true的比较将项目移动到底部,因此我们需要用ab切换支票。

The second part 第二部分

a.price - b.price

is only evaluated if the previous delta is zero, then the delta of price is returned. 仅在先前的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); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM