Here is an array of objects I am working with:
const items = [
{ name: "Different Item", amount: 100, matches: 2 },
{ name: "Different Item", amount: 100, matches: 2 },
{ name: "An Item", amount: 100, matches: 1 },
{ name: "Different Item", amount: 30, matches: 2 }
]
I have a requirement to sort these objects based on both the matches
and amount
properties. The expected result after sorting should be as follows:
[
{ name: "Different Item", amount: 100, matches: 2 },
{ name: "Different Item", amount: 100, matches: 2 },
{ name: "Different Item", amount: 30, matches: 2 },
{ name: "An Item", amount: 100, matches: 1 }
]
The primary sorting criteria is based on the matches
property, followed by sorting based on the amount
property within each group of identical matches
. While I am aware that we can individually sort by either matches
or amount
, such as:
items.sort((a, b) => a.matches - b.matches);
I need guidance on how to perform sorting based on both properties simultaneously.