1

How would I sort an array of objects using the values in the desiredOrder array? I want my array objects to be sorted by the type key in each object. I want all the fruit items to show first, then the drug items, then the wheat items. Etc. It would be preferred if the function was a reusable one.

// This is the order I want myList sorted in by the object property type
let desiredOrder = ['fruit', 'drugs', 'wheat', 'candy', 'vegetable', 'dairy']



// Here is my list of objects
let myList = [
    {
        type: "fruit",
        subType: "apple"
    },
    {
        type: "vegetable",
        subType: "carrot"
    },
    {
        type: "fruit",
        subType: "orange"
    },
    {
        type: "dairy",
        subType: "milk"
    },
    {
        type: "wheat",
        subType: "bread"
    },
    {
        type: "vegetable",
        subType: "lettuce"
    },
    {
        type: "fruit",
        subType: "mango"
    },
    {
        type: "drugs",
        subType: 'cocaine'
    },
    {
        type: "vegetable",
        subType: "spinach"
    },
    {
        type: "candy",
        subType: "chocolate"
    },
    {
        type: "vegetable",
        subType: "celery"
    },
    {
        type: "fruit",
        subType: "pineapple"
    },
]




2 Answers 2

2

The function

(a, b) => (desiredOrder.indexOf(a.type) - desiredOrder.indexOf(b.type)) || (a.subType > b.subType ? 1 : -1) || 0;

Will order first on the order the type is in the array and then by the subType if they have the same type.

let desiredOrder = ['fruit', 'drugs', 'wheat', 'candy', 'vegetable', 'dairy']



// Here is my list of objects
let myList = [
    {
        type: "fruit",
        subType: "apple"
    },
    {
        type: "vegetable",
        subType: "carrot"
    },
    {
        type: "fruit",
        subType: "orange"
    },
    {
        type: "dairy",
        subType: "milk"
    },
    {
        type: "wheat",
        subType: "bread"
    },
    {
        type: "vegetable",
        subType: "lettuce"
    },
    {
        type: "fruit",
        subType: "mango"
    },
    {
        type: "drugs",
        subType: 'cocaine'
    },
    {
        type: "vegetable",
        subType: "spinach"
    },
    {
        type: "candy",
        subType: "chocolate"
    },
    {
        type: "vegetable",
        subType: "celery"
    },
    {
        type: "fruit",
        subType: "pineapple"
    },
];

const compareTypeThenSubtype = (a, b) => (desiredOrder.indexOf(a.type) - desiredOrder.indexOf(b.type)) || (a.subType > b.subType ? 1 : -1) || 0;

myList.sort(compareTypeThenSubtype);

console.log(myList);

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

Comments

1

You need to use sort()

myList.sort((preItem, nextItem) =>
  desiredOrder.indexOf(preItem.type) > desiredOrder.indexOf(nextItem.type) ? 1 : -1
);

Comments

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.