Array Sort - Not sorting
Array Sort - Not sorting
JavaScript
var data = {
"attributes": [
{
"value": "123-456-7890",
"name": "phone",
"orderNum": 4
},
{
"value": "[email protected]",
"name": "email",
"orderNum": 3
},
{
"value": "Gotham",
"name": "city",
"orderNum": 1
},
{
"value": "12",
"name": "ID",
"orderNum": 5
},
{
"value": "Batman",
"name": "Super Hero",
"orderNum": 2
}
]
};
data.attributes.sort( function (a, b) {
console.log(a);
if ( (typeof b.orderNum === 'undefined' && typeof a.orderNum !== 'undefined') || a.orderNum < b.orderNum ) {
return -1;
}
if ( (typeof a.orderNum === 'undefined' && typeof b.orderNum !== 'undefined') || a.orderNum > b.orderNum ) {
return 1;
}
return 0;
});
console.log(data);