If you had
const arr = [
{
id: 1,
type: 'input',
val: 1
},
{
id: 2,
type: 'boolean',
val: 3
},
{
id: 3,
type: 'choice',
val: 4,
},
{
id: 1,
type: 'select',
val: 2
},
]
then it would be easy: just arr.sort((a, b) => a.val - b.val)
Since you don't have the property val
, you can just set it on before:
const typeToValue = {
input: 1,
boolean: 3,
choice: 4,
select: 2
}
arr.forEach(el => {
el.val = typeToValue[el.type]
})
arr.sort((a, b) => a.val - b.val)
Maybe you don't want to dirty your elements, notice that el.val == typeToValue[el.type]
Meaning you can write
arr.sort((a, b)=>typeToValue[a.type] - typeToValue[b.type])
Finally should you have a sorted array ['input', 'select', 'boolean', 'choice']
you can trivially transform it to typeToValue
object via Array.prototype.reduce
const orders = ['input', 'select', 'boolean', 'choice']
const typeToValue = orders.reduce((o, el, i) => (o[el] = i, o), {})
or if you don't like reduce with Object.fromEntries
const typeToValue = Object.fromEntries(orders.map((el, i) => [el, i]))
const arr = [{"id":1,"type":"input"},{"id":2,"type":"boolean"},{"id":3,"type":"choice"},{"id":1,"type":"select"}]
const orders = ['input', 'select', 'boolean', 'choice']
const typeToValue1 = orders.reduce((o, el, i) => (o[el] = i, o), {})
const typeToValue2 = Object.fromEntries(orders.map((el, i) => [el, i]))
// just slice to copy array because sort modify in place
console.log(arr.slice(0).sort((a, b)=>typeToValue1[a.type] - typeToValue1[b.type]))
console.log(arr.slice(0).sort((a, b)=>typeToValue2[a.type] - typeToValue2[b.type]))