-4

Have Object. look like this

enter image description here

need to sort it, in result: mo,tu,we,th,fr,st,su

try do this but not working

const sortOrder = {'mo': 1, 'tu': 2, 'we': 3, 'th':4,'fr':5, 'sa':6, 'su':7}
      const res = result.map(o => Object.assign({}, ...Object.keys(o).sort((a, b) => sortOrder[a] - sortOrder[b]).map(x => { return { [x]: o[x]}})))

1 Answers1

4

To complicated. Much easier:

var days = ["mo","tu","we","th","fr","sa","su"];

result.sort((a,b)=> days.indexOf(a.day) - days.indexOf(b.day));
Jonas Wilms
  • 106,571
  • 13
  • 98
  • 120