admin管理员组文章数量:1332207
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 acplish 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
Ining 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 ining has more values than the order list.
Ining 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.
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 acplish 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
Ining 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 ining has more values than the order list.
Ining 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.
Share Improve this question edited Jan 14, 2020 at 19:59 Coogie7 asked Jan 14, 2020 at 19:33 Coogie7Coogie7 1993 silver badges12 bronze badges 8- 1 why not take the order array? – Nina Scholz 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. – Coogie7 Commented Jan 14, 2020 at 19:35
- Will there be duplicates? – spender Commented Jan 14, 2020 at 19:39
- @spender - No there will not be duplicates. – Coogie7 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." – Coogie7 Commented Jan 14, 2020 at 19:41
2 Answers
Reset to default 9You 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);
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);
本文标签: javascriptOrdering an array of strings according to a specific arrangement of stringsStack Overflow
版权声明:本文标题:javascript - Ordering an array of strings according to a specific arrangement of strings - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742244491a2439042.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论