1

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 {
    //??
    }
});
2
  • 2
    Low, Normal and High should be comparable somehow to sort. Try to map them to integers and then sort by the interger, which will make it easier. Commented May 11, 2018 at 11:07
  • OK, if you have this var importanceOrder = ['Low', 'Normal', 'High'];, and you have, say, a variable ``a = "Low"` then another that b = "High". With this information alone, how would you check if a > b? Commented May 11, 2018 at 11:15

2 Answers 2

2

You could take an object for the sortorder of imortance and a function for getting an ISO date.

const getISO = s => s.replace(/(..)\/(..)\/(....)/, '$3-$1-$2');

var array = [{ 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' }],
    order = { Low: 1, Normal: 2, High: 3 };


array.sort((a, b) => getISO(a.DOB).localeCompare(getISO(b.DOB)) || order[a.importance] - order[b.importance]);

console.log(array);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Sign up to request clarification or add additional context in comments.

Comments

-1

Ok, this is pretty straightforward, firstly check whether dates are different and sort them in that manner and then if they are equal (x - x == 0 which will return false in first equation) check their importance, easy as that :)

arr.sort((a, b) => new Date(a.DOB) - new Date(b.DOB) || importanceOrder.indexOf(a.importance) - importanceOrder.indexOf(b.importance))

3 Comments

new Date(a.DOB) -- this doesn't return what you expect. Try it on console. The bahaviour is not consistent among the browsers.
@31piy couldn't assume what the format is, because OP posted all dates with 01/01, so it returns exactly what I expected, I just picked American date notation with "month-day-year" order
You're not the only one who gets confused at it, mate. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.