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