You can try to sort your object props without creating an array, but it will cost you more lines of code to write, and you have to watch for your properties, so they didn't inherited from other objects. So you have to use for in loop:
for (var prop in obj) {
if(obj.hasOwnProperty(prop)) {
//perform sorting...
}
}
Or you can transform your object into an array and sort data via standard js array sorting function:
function sortObj(obj) {
var objToArr = [],
prop,
sortedObj = {};
for (prop in obj) {
if (obj.hasOwnProperty(prop)) {
objToArr.push([prop, obj[prop]]);
}
}
objToArr.sort(function(a, b) { return a[0] - b[0]; });
objToArr.forEach(function (val, key) {
sortedObj[val[0]] = sortedObj[val[1]];
});
return sortedObj;
}
You can adopt this function for more complex objects, for example when one property of object contains another object, etc.