3

In Javascript, I have an array of values such as:

["Toy","Car","PC","Water","Apple"]

I need to order the values a specific order in this priority:

PC, Car, Toy, Apple, Water

Note, this cannot be sorted alphabetic, but in a specific order of values provided.

How would I best accomplish this? After it is ordered, I will be iterating it in a FOR loop, and needs to stay in that order. Also, keeping it in an array would be best.

Also, if we can add any values not specified in the "priority order list" to be added on at the end of the resulting array, alphabetically, that would be ideal.

Sometimes the passed array will not contain all the values specified in the priority order list, hence why I cannot use the priority order list itself.

Example 2

Incoming array: ["Green","Blue","Yellow","Red"]

Provided preferred order of values: Yellow, Black, Silver, Blue, Red, Green, Grey

Expected output: ["Yellow","Blue","Red","Green"]

Example 3

In this case, the incoming has more values than the order list.

Incoming array: ["Green","Blue","Yellow","Red","Silver","Black]

Provided preferred order of values: Yellow, Black, Silver

Expected output: ["Yellow","Black","Silver","Green","Blue","Red"]

The additional values should be appended to the outputted list, preferably in alphabetical order for the extra values.

8
  • 1
    why not take the order array? Commented Jan 14, 2020 at 19:34
  • Because they may not aways be in the array. I could be given a list of 10 values to order by, but sometimes the passed set of values could be only 4 of them. Commented Jan 14, 2020 at 19:35
  • Will there be duplicates? Commented Jan 14, 2020 at 19:39
  • @spender - No there will not be duplicates. Commented Jan 14, 2020 at 19:40
  • 1
    Sorry @Ruzihm. I'll do my best. I'm being passed an array of values into a Javascript function. However, the order of the values needs to be altered according to a list of values my business partners prefer. It wouldn't be alphabetical, but really an order that makes sense to them. The array of values passed into the Javascript function wouldn't necessarily always include all values mentioned in their "preferred order." Commented Jan 14, 2020 at 19:41

2 Answers 2

9

You could take an object with the wanted order and specify a default value for unknon items.

var data = ["Toy", "Car", "PC", "Water", "Apple", "Sky", "Banana", "Day", "Green"],
    order = { PC: 1, Car: 2, Toy: 3, Apple: 4, Water: 5, default: Number.MAX_VALUE };

data.sort((a, b) =>
    (order[a] || order.default) - (order[b] || order.default) ||
    a > b || -(a < b)
);

console.log(...data);

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

8 Comments

Nina, you're really fast. How would this solution work if the "data" array included values not in the order list? Is that what "default" value would do?
yes. this value can be any other value, depending on the wanted positions.
Thanks. I'm willing to make a compromise and not include this functionality, but is there a way to alphabetize those extra values that will use "default"?
Nina is very very fast. :)
you could add a chained part for sorting items who are equal (getting a zero) by string or number.
|
1

You could create a priority map and use a simple .sort()

const priorityOrder = {
  PC: 0,
  Car: 1,
  Toy: 2,
  Apple: 3,
  Water: 4,
  Banana: 5,
  Bicycle: 6
};

const input = ["Apple", "Car", "Earth", "Water", "Toy", "PC", "Plane", "Alpha Centauri"];

const prioritized = [];
const extra = [];

// to be more efficient I will use a forEach, not .filter() x2
input.forEach((value) => {
  if (priorityOrder[value] !== undefined)
    prioritized.push(value);
  else
    extra.push(value);
});

prioritized.sort((a, b) => priorityOrder[a] - priorityOrder[b]);
extra.sort();

const output = [].concat(prioritized, extra);
console.log(output);

2 Comments

Sebastian, you're really fast. How would this solution work if the "input" array included values not in the priority order?
Ok, I just saw your edited comment that such a situation is possible. I edited my answer. Now it will sort extra values alphabetically but as I can see @Nina's solution is better because it uses only one .sort().

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.