I have an array of objects:
arr = [
{
name : 'Gary',
DOB : '01/01/1980',
importance: 'High'
},
{
name : 'Bill',
DOB : '01/01/1985',
importance: 'High'
},
{
name : 'Joe',
DOB : '01/01/1981',
importance: 'Medium'
},
{
name : 'Phillip',
DOB : '01/01/1981',
importance: 'High'
},
{
name : 'Ed',
DOB : '01/01/1980',
importance: 'Low'
},
{
name : 'Nix',
DOB : '01/01/1980',
importance: 'High'
},
{
name : 'Pat',
DOB : '01/01/1980',
importance: 'Low'
}
]
I'm sorting these guys by two fields, first by DOB, and if they have the same DOB, then by importance, Low to High, so Low at the top and High at the bottom.
I can compare their dates easily enough, but I'm having a hard time ordering them by their importance after that?
Can anyone offer any advice?
var importanceOrder = ['Low', 'Normal', 'High'];
arr.sort(function(a, b) {
if(a.dueDate.value != b.dueDate.value){
return a.dueDate.value < b.dueDate.value ? -1 : 1;
} else {
//??
}
});
Low,NormalandHighshould be comparable somehow to sort. Try to map them to integers and then sort by the interger, which will make it easier.var importanceOrder = ['Low', 'Normal', 'High'];, and you have, say, a variable ``a = "Low"` then another thatb = "High". With this information alone, how would you check ifa > b?