1function sortObjectByKeys(o) {
2 return Object.keys(o).sort().reduce((r, k) => (r[k] = o[k], r), {});
3}
4var unsorted = {"c":"crane","b":"boy","a":"ant"};
5var sorted=sortObjectByKeys(unsorted); //{a: "ant", b: "boy", c: "crane"}
1/**
2 * Function to sort alphabetically an array of objects by some specific key.
3 *
4 * @param {String} property Key of the object to sort.
5 */
6function dynamicSort(property) {
7 var sortOrder = 1;
8
9 if(property[0] === "-") {
10 sortOrder = -1;
11 property = property.substr(1);
12 }
13
14 return function (a,b) {
15 if(sortOrder == -1){
16 return b[property].localeCompare(a[property]);
17 }else{
18 return a[property].localeCompare(b[property]);
19 }
20 }
21}