Use String.localeCompare()
with the numeric
option to sort using possibility
, but use -
(minus) to get descending results. If the comparison returns 0 (equals), use localeCompare
again to compare the names:
const array = [{"name":"fever","possibility":"20%"},{"name":"hiv","possibility":"25%"},{"name":"heart-attack","possibility":"20%"},{"name":"covid","possibility":"40%"}]
const result = array.sort((a, b) =>
-a.possibility.localeCompare(b.possibility, undefined, { numeric: true })
||
a.name.localeCompare(b.name)
)
console.log(result)
solved Sort out array of object using its value [duplicate]