I have the following array
let arr = [
{
auctionProductName: "m",
pricePerUnitPerHour: 1,
quanitity:20
},
{
auctionProductName: "m",
pricePerUnitPerHour: 22,
quanitity:20
},
{
auctionProductName: "a",
pricePerUnitPerHour: 5555,
quanitity:20
},
{
auctionProductName: "a",
pricePerUnitPerHour: 22,
quanitity:20
},
{
id:1,
auctionProductName: "a",
pricePerUnitPerHour: 22,
quanitity:20
},
{
auctionProductName: "m",
pricePerUnitPerHour: 2222,
quanitity:20
},
{
id:2,
auctionProductName: "a",
pricePerUnitPerHour: 22,
quanitity:2
},
]
so this array need to be sorted firstly ASCENDING on auctionProductName, if there are same objects after this sorting by auctionProductName then they need to be sorted by pricePerUnitPerHour.
For that i have the following code which works as expected
function defaultTableSort() {
arr = arr.sort(
function (a, b) {
if (a.auctionProductName === b.auctionProductName) {
return a.pricePerUnitPerHour - b.pricePerUnitPerHour;
}
return a.auctionProductName > b.auctionProductName ? 1 : -1;
});
}
after the sorting i get
[
{
"auctionProductName": "a",
"pricePerUnitPerHour": 22,
"quanitity": 20
},
{
"id": 1,
"auctionProductName": "a",
"pricePerUnitPerHour": 22,
"quanitity": 20
},
{
"id": 2,
"auctionProductName": "a",
"pricePerUnitPerHour": 22,
"quanitity": 2
},
{
"auctionProductName": "a",
"pricePerUnitPerHour": 5555,
"quanitity": 20
},
{
"auctionProductName": "m",
"pricePerUnitPerHour": 1,
"quanitity": 20
},
{
"auctionProductName": "m",
"pricePerUnitPerHour": 22,
"quanitity": 20
},
{
"auctionProductName": "m",
"pricePerUnitPerHour": 2222,
"quanitity": 20
}
]
i can't find a way to modify my defaultTableSort
function - so the third condition will be by quantity -
if the auctionProductName and pricePerUnitPerHour are same, than we should sort by quantity.
That means that the third object ( AFTER THE SORTING ) with id of 2 needs to be before the second object with id of 1.
CodePudding user response:
You could chain the wanted sorting.
For getting a descending sorting, you could exchange a
and b
.
const
array = [{ auctionProductName: "m", pricePerUnitPerHour: 1, quanitity: 20 }, { auctionProductName: "m", pricePerUnitPerHour: 22, quanitity: 20 }, { auctionProductName: "a", pricePerUnitPerHour: 5555, quanitity: 20 }, { auctionProductName: "a", pricePerUnitPerHour: 22, quanitity: 20 }, { id: 1, auctionProductName: "a", pricePerUnitPerHour: 22, quanitity: 20 }, { auctionProductName: "m", pricePerUnitPerHour: 2222, quanitity: 20 }, { id: 2, auctionProductName: "a", pricePerUnitPerHour: 22, quanitity: 2 }];
array.sort((a, b) =>
a.auctionProductName.localeCompare(b.auctionProductName) ||
a.pricePerUnitPerHour - b.pricePerUnitPerHour ||
a.quanitity - b.quanitity
);
console.log(array);
.as-console-wrapper { max-height: 100% !important; top: 0; }
CodePudding user response:
Just add another if clause
function (a, b) {
if (a.auctionProductName === b.auctionProductName) {
if(a.pricePerUnitPerHour === a.pricePerUnitPerHour)
return a.quantity - b.quantity;
return a.pricePerUnitPerHour - b.pricePerUnitPerHour;
}
return a.auctionProductName > b.auctionProductName ? 1 : -1;
}
CodePudding user response:
You can other conditional structure for verify if:
a.pricePerUnitPerHour === b.pricePerUnitPerHour
with a final code:
function defaultTableSort() {
arr = arr.sort(
function (a, b) {
if (a.auctionProductName === b.auctionProductName) {
// auctionProductName is equal between a and b
if (a.pricePerUnitPerHour === b.pricePerUnitPerHour) {
// pricePerUnitPerHour is equal between a and b
// sort based on: quantity
return a.quantity - b.quantity
}
// sort based on: pricePerUnitPerHour
return a.pricePerUnitPerHour - b.pricePerUnitPerHour;
}
// sort based on: auctionProductName
return a.auctionProductName > b.auctionProductName ? 1 : -1;
});
}
CodePudding user response:
Rather than nesting if statements you could
function defaultTableSort() {
return arr = arr.sort(
function (a, b) {
if (a.auctionProductName === b.auctionProductName && a.pricePerUnitPerHour === b.pricePerUnitPerHour) {
return a.quanitity - b.quanitity;
}
if (a.auctionProductName === b.auctionProductName) {
return a.pricePerUnitPerHour - b.pricePerUnitPerHour;
}
return a.auctionProductName > b.auctionProductName ? 1 : -1;
});
}
And that has the output
[{
"id": 2,
"auctionProductName": "a",
"pricePerUnitPerHour": 22,
"quanitity": 2
}, {
"auctionProductName": "a",
"pricePerUnitPerHour": 22,
"quanitity": 20
}, {
"id": 1,
"auctionProductName": "a",
"pricePerUnitPerHour": 22,
"quanitity": 20
}, {
"auctionProductName": "a",
"pricePerUnitPerHour": 5555,
"quanitity": 20
}, {
"auctionProductName": "m",
"pricePerUnitPerHour": 1,
"quanitity": 20
}, {
"auctionProductName": "m",
"pricePerUnitPerHour": 22,
"quanitity": 20
}, {
"auctionProductName": "m",
"pricePerUnitPerHour": 2222,
"quanitity": 20
}]
CodePudding user response:
const arr = [
{
auctionProductName: 'm',
pricePerUnitPerHour: 1,
quanitity: 20,
},
{
auctionProductName: 'm',
pricePerUnitPerHour: 22,
quanitity: 20,
},
{
auctionProductName: 'a',
pricePerUnitPerHour: 5555,
quanitity: 20,
},
{
auctionProductName: 'a',
pricePerUnitPerHour: 22,
quanitity: 20,
},
{
id: 1,
auctionProductName: 'a',
pricePerUnitPerHour: 22,
quanitity: 20,
},
{
auctionProductName: 'm',
pricePerUnitPerHour: 2222,
quanitity: 20,
},
{
id: 2,
auctionProductName: 'a',
pricePerUnitPerHour: 22,
quanitity: 2,
},
];
const orderBy = (arr, props, orders) =>
[...arr].sort((a, b) =>
props.reduce((acc, prop, i) => {
if (acc === 0) {
const [p1, p2] = orders && orders[i] === 'desc' ? [b[prop], a[prop]] : [a[prop], b[prop]];
acc = p1 > p2 ? 1 : p1 < p2 ? -1 : 0;
}
return acc;
}, 0)
);
console.log(
JSON.stringify(
orderBy(arr, ['auctionProductName', 'pricePerUnitPerHour', 'quanitity'] /*, ['desc', 'desc', 'desc']*/),
null,
2
)
);