I have a dataset that consists of different arrays of objects. When I log myData, the output looks something like this:
[
{
name: 'Cdb',
image: 'xxx',
coll: 'xxx'
},
{
name: 'Bcd',
image: 'xxx',
coll: 'xxx',
url: 'myurl'
}
]
[
{
name: 'Abc',
image: 'xxx',
coll: 'xxx'
}
]
My goal is to sort these objects alphabetically based on the value of name
. Additionally, if an object has the parameter url
, it should be placed at the beginning of the sorted list.
The desired output should look like this:
name: 'Cdb',
image: 'xxx',
coll: 'xxx',
url: 'myurl'
name: 'Abc',
image: 'xxx',
coll: 'xxx'
name: 'Bcd',
image: 'xxx',
coll: 'xxx'
I've attempted various approaches, including the following code snippet, but none seem to work as expected:
var myData = props?.data
console.log(props?.data)
if(props.data){
// Sort with objects having url first and then alphabetically
myData = props.data.sort(function(a, b){
return b.url ? 1 || (a.name < b.name) : -1;
})
}