I encountered a situation where I need to reorder items in an array based on the property minLVL
of each object
within the array.
However, I have discovered that this reordering only works if the previous and next items have the required minLVL
field. If an object is missing the minLVL
property, it remains in its original position instead of being moved to the bottom of the list.
How can I resolve this issue? Thank you
Example:
var h = [
{
ID: 172
},
{
ID: 179,
minLVL: "30"
},
{
ID: 169
},
{
ID: 173
},
{
ID: 167,
minLVL: "25"
},
{
ID: 175,
minLVL: "10"
}
]
var n = h.sort((a, b) => Number(b.minLVL) - Number(a.minLVL))
console.log(n)