JS

HOW TO SORT OBJECTS IN JAVASCRIPT

1) SORT BY KEY

THE OBJECT var obj = { bow:"Bar", arrow:"Foo" };   EXTRACT & SORT KEYS var keys = Object.keys(obj); keys.sort();   “MAP” KEYS BACK TO OBJECT var sorted = {}; for (let k of keys) { sorted[k] = odata[k]; }

THE OBJECT var obj = { bow:"Bar", arrow:"Foo" };   EXTRACT & SORT ENTRIES var entries = Object.entries(obj); entries.sort(function(a, b){   return a[1].localeCompare(b[1]); });   “MAP” BACK TO OBJECT var sorted = {}; for (let i of entries) { sorted[i[0]] = i[1]; }

2) SORT BY VALUE