I want o sort an array of objects with a specific order given from a sort priority text.
var sortPriority = "ramoklyxq";
items[
0:{data: "aizak", level: 'z'}
1:{data: "alice", level: 'r'}
2:{data: "bob", level: 'a'}
3:{data: "trinity", level: 'm'}
4:{data: "ella", level: 'z'}
5:{data: "John", level: 'z'}
6:{data: "bryan", level: 'z'}
7:{data: "sam", level: 'o'}
8:{data: "nick", level: 'k'}
9:{data: "angel", level: 'l'}
10:{data: "arla", level: 'z'}
11:{data: "marley", level: 'o'}
etc
]
i want to return as:
items[
0:{data: "alice", level: 'r'}
1:{data: "bob", level: 'a'}
2:{data: "trinity", level: 'm'}
3:{data: "marley", level: 'o'}
4:{data: "sam", level: 'o'}
5:{data: "nick", level: 'k'}
6:{data: "angel", level: 'l'}
7:{data: "aizak", level: 'z'}
8:{data: "arla", level: 'z'}
9:{data: "bryan", level: 'z'}
10:{data: "ella", level: 'z'}
11:{data: "John", level: 'z'}
etc
]
Is there any way to sort the list by data.level order from sortPriority level.
If there no level exists in the sortPriority will be added after as in the return example.
I use this function first to sort with level and after with data.
for (let i = 0; i < items.length; i++) {
var list = document.getElementById('holder');
[...list.children]
.sort((a, b) => a.data.level > b.data.level ? 1 : -1)
.forEach(node => list.appendChild(node));
for (let j = 0; j < items.length; j++) {
var list = document.getElementById('holder');
[...list.children]
.sort((a, b) => a.data.data > b.data.data ? 1 : -1)
.forEach(node => list.appendChild(node));
}
}
I need something like this function with the correct priority in ASC for each level’s results so i can append it to list as shows
Thanks in advance.
2
Answers
You could take an object for the order and sort accordingly. For same priority sort by string.