1

Say I have an order which is just an ordered list that references the array of objects with a sectionId. I'd like the items missing from the order list to be appended to the end of the sorted array. However my example below the missing items are put at the top of the list - help appreciated!

const orders = [
[
    "one", 
    "two",
    "three",
],
[
    "two",
    "three",
    "one", 
],
[
    "three",
    "one", 
    "two",
]
]

const sections = {
0: [
    {
        "sectionId": "a"
    }, 
    {
        "sectionId": "two"
    },
    {
        "sectionId": "z"
    },
    {
        "sectionId": "three"
    },
    {
        "sectionId": "one"
    }, 
],
1: [
    {
        "sectionId": "a"
    },
    {
        "sectionId": "three"
    },
    {
        "sectionId": "one"
    }, 
    {
        "sectionId": "z"
    }, 
    {
        "sectionId": "two"
    },
], 
2: [
    {
        "sectionId": "z"
    },
    {
        "sectionId": "a"
    }, 
    {
        "sectionId": "two"
    },
    {
        "sectionId": "three"
    },
    {
        "sectionId": "one"
    }, 
]
}

const outcomes = {
0: [
    {
        "sectionId": "one"
    }, 
    {
        "sectionId": "two"
    },
    {
        "sectionId": "three"
    },
    {
        "sectionId": "a"
    },
    {
        "sectionId": "z"
    }, 
],
1: [
    {
        "sectionId": "two"
    },
    {
        "sectionId": "three"
    },
    {
        "sectionId": "one"
    },
    {
        "sectionId": "a"
    },
    {
        "sectionId": "z"
    }, 
],
2: [
    {
        "sectionId": "three"
    },
    {
        "sectionId": "one"
    }, 
    {
        "sectionId": "two"
    },
    {
        "sectionId": "z"
    },
    {
        "sectionId": "a"
    }, 
],
}

// function
const generateOrderedSections = (sections, order) => {
return sections.sort((a, b) => {
    const aIndex = order.indexOf(a.sectionId)
    const bIndex = order.indexOf(b.sectionId)
    const sortOrder = aIndex > 0 && aIndex > bIndex ? 1 : -1
    // console.log(aIndex, a.sectionId, bIndex, b.sectionId, sortOrder)
    return sortOrder;
})
}

// test cases
orders.forEach((order, index) => {
const section = sections[index]
const test = generateOrderedSections(section, order)
const result = JSON.stringify(test)
const outcome = JSON.stringify(outcomes[index])

if (result === outcome) {
    console.log(index, 'passed')
} else {
    console.log('-------------')
    console.log('failed')
    console.log('-------------')
    console.log('expected')
    console.log(outcomes[index])
    console.log('\nresult')
    console.log(test)
}
})

2
  • why comes 'z' before 'a' in thei first part? Commented Nov 29, 2019 at 12:30
  • Fixing the test case now thanks Commented Nov 29, 2019 at 12:31

1 Answer 1

1

You could add one tho the index, and this yields zero (for unknow indices) and take a default value of a large number to sort this items to the end.

const
    orders = [["one", "two", "three"], ["two", "three", "one"], ["three", "one", "two"]],
    sections = { 0: [{ sectionId: "a" }, { sectionId: "two" }, { sectionId: "z" }, { sectionId: "three" }, { sectionId: "one" }], 1: [{ sectionId: "a" }, { sectionId: "three" }, { sectionId: "one" }, { sectionId: "z" }, { sectionId: "two" }], 2: [{ sectionId: "z" }, { sectionId: "a" }, { sectionId: "two" }, { sectionId: "three" }, { sectionId: "one" }] },
    outcomes = { 0: [{ sectionId: "one" }, { sectionId: "two" }, { sectionId: "three" }, { sectionId: "a" }, { sectionId: "z" }], 1: [{ sectionId: "two" }, { sectionId: "three" }, { sectionId: "one" }, { sectionId: "a" }, { sectionId: "z" }], 2: [{ sectionId: "three" }, { sectionId: "one" }, { sectionId: "two" }, { sectionId: "z" }, { sectionId: "a" }] },
    generateOrderedSections = (sections, order) => {
        return sections.sort((a, b) =>
            ((order.indexOf(a.sectionId) + 1) || Number.MAX_VALUE) -
            ((order.indexOf(b.sectionId) + 1) || Number.MAX_VALUE)
        );
    };

// test cases
orders.forEach((order, index) => {
    const section = sections[index]
    const test = generateOrderedSections(section, order)
    const result = JSON.stringify(test)
    const outcome = JSON.stringify(outcomes[index])

    if (result === outcome) {
        console.log(index, 'passed')
    } else {
        console.log('-------------')
        console.log('failed')
        console.log('-------------')
        console.log('expected')
        console.log(outcomes[index])
        console.log('\nresult')
        console.log(test)
    }
})
.as-console-wrapper { max-height: 100% !important; top: 0; }

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

3 Comments

Thanks, I seem to get all results ordered the same as: one, two, three unfortunately
sorry, i missed some parentheses.
Thank you :) Great job makes sense and is concise

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.