Below is my sort function but I want to display the ones with a code to be at the top. I want to sort my array of objects by code, then by description. Right now, the items without a code is being placed at the top of the order.
data = [
{
code: "1.1",
description: "aaa"
},
{
code: "",
description: "bbb"
},
{
code: "1.2",
description: "ccc"
}
]
let sortedData = data.sort(function(a, b) {
let codeA = a.code
let codeB = b.code
let descriptionA = a.description.toLowerCase()
let descriptionB = b.description.toLowerCase()
if (codeA < codeB) return -1
if (codeA > codeB) return 1
if (descriptionA < descriptionB) return -1
if (descriptionA > descriptionB) return 1
return 0
})
return sortedData
Current order:
["bbb", "aaa", "ccc"]
Expected order:
["aaa", "ccc", "bbb"]