2

I have an array returned by a REST query that represents the number of items in a multi-step production process.

var steps = [
  { name:'Package', value:3},
  { name:'Assemble', value:1 },
  { name:'Ship', value:7},
  { name:'Preprocess', value:9 },
  { name:'Paint', value:5 }
];

I'd like to sort them in the order of the process, like this:

  1. Preprocess
  2. Paint
  3. Assemble
  4. Package
  5. Ship

I have other alphanumeric sorts that I am doing with Underscore but I cannot figure this one out.

5
  • Attach a sort order field to the objects and sort on that. Or keep a sorting order array separately or something. You can't do it without information on how these are to be sorted. Commented Oct 20, 2018 at 11:55
  • Do you want to order these by value? Commented Oct 20, 2018 at 11:56
  • 1
    @Matthi Preprocess has a value of 9 but it's first, so no. Commented Oct 20, 2018 at 12:06
  • @vlaz, I'd like them sorted as I show in my question by steps 1-5. Commented Oct 20, 2018 at 13:50
  • @vlaz True, that's the point. Was not sure where the order value would come from except from another array. But it is fortunately solved 👍 Commented Oct 20, 2018 at 15:31

3 Answers 3

2

You could take an object for the wanted order with numerical values for the position. Then sort by this values.

var steps = [{ name: 'Package', value: 3 }, { name: 'Assemble', value: 1 }, { name: 'Ship', value: 7 }, { name: 'Preprocess', value: 9 }, { name: 'Paint', value: 5 }],
    order = { Preprocess: 1, Paint: 2, Assemble: 3, Package: 4, Ship: 5 };
    
steps.sort(({ name: a }, { name: b }) => order[a] - order[b]);

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

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

Comments

0

You can create an object which mentions the priority on each name while ordering.

let priority = {
  Preprocess: 1,
  Paint: 2,
  Assemble: 3,
  Package: 4,
  Ship: 5,
}

var steps = [
  { name:'Package', value:3},
  { name:'Assemble', value:1 },
  { name:'Ship', value:7},
  { name:'Preprocess', value:9 },
  { name:'Paint', value:5 }
];

const result = steps.sort((a,b) => priority[a.name] - priority[b.name]);

console.log(result);

Comments

0

I'm just using a precedence map for every step type so that I can use it in the compare function.

var steps = [
    { name: 'Package', value: 3 },
    { name: 'Assemble', value: 1 },
    { name: 'Ship', value: 7 },
    { name: 'Preprocess', value: 9 },
    { name: 'Paint', value: 5 }
];
var stepPrecedenceMap = new Map([["Preprocess", 1], ["Paint", 2], ["Assemble", 3], ["Package", 4], ["Ship", 5]])

console.log(
steps.sort((stepA, stepB) => {  
return stepPrecedenceMap.get(stepA.name) - stepPrecedenceMap.get(stepB.name)
}));

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.