fork download
  1. var data = [
  2. {
  3. id: 1,
  4. state: "Pending"
  5. },
  6. {
  7. id: 2,
  8. state: "Done"
  9. },
  10. {
  11. id: 3,
  12. state: "Busy"
  13. }
  14. ];
  15.  
  16. var sortOrders = {
  17. "Done": 1,
  18. "Busy": 2,
  19. "Pending": 3
  20. };
  21.  
  22. console.log(data);
  23.  
  24. data.sort(function(a, b) {
  25. return sortOrders[a.state] - sortOrders[b.state];
  26. });
  27.  
  28. console.log(data);
Success #stdin #stdout 0.06s 10968KB
stdin
Standard input is empty
stdout
[ { id: 1, state: 'Pending' },
  { id: 2, state: 'Done' },
  { id: 3, state: 'Busy' } ]
[ { id: 2, state: 'Done' },
  { id: 3, state: 'Busy' },
  { id: 1, state: 'Pending' } ]