Answers for "sort objects by another array JS"

0

sort objects by another array JS

const machines = [
  { id: 1, client: 'Machine A', state: 'Ready' },
  { id: 2, client: 'Machine B', state: 'Stopped' },
  { id: 3, client: 'Machine C', state: 'Active' },
  { id: 4, client: 'Machine D', state: 'Stopped' },
  { id: 5, client: 'Machine E', state: 'Active' },
  { id: 6, client: 'Machine F', state: 'Stopped' }
]

const sortedBy = {
  'Active'  : 0, 
  'Ready'   : 1, 
  'Stopped' : 2,
}

const result = machines.sort(
  (a, b) => sortedBy[a.state] - sortedBy[b.state]
)
Posted by: Guest on August-24-2021

Code answers related to "sort objects by another array JS"

Code answers related to "Javascript"

Browse Popular Code Answers by Language