UPDATE: Thanks to a heated but useful exchange with Doorknob, I realized that he had an excellent point about being careful about modifying Object.prototype. I modified the answer to make the extension of Object.prototype less intrusive by making the new method non-enumerable. Thanks, Doorknob!
UPDATE 2: I realize now that the OP's wisely selected correct answer has the right of it. Object.keys does exactly what my ownProps method (see below) does, and it's a standard (though you still have to polyfill in IE8). I would just delete this answer since cookie monster nailed it, but I think this is a useful discussion and even though ownProps is redundant with Object.keys, it might teach someone...something.
I wrote a convenience function called ownProps which returns all the property names of an object (excluding properties in the prototype chain). It's a more functional way of doing what Doorknob is suggesting above:
// retrieves all of the properties of an object as an array,
// omitting properties in the prototype chain; note that we use
// Object.defineProperty so we can make this non-enumerable, thereby
// making a safer modification of Object.prototype
Object.defineProperty(Object.prototype,'ownProps',{
enumerable: false,
value: function() {
var props = []; for( var prop in this ) {
if( this.hasOwnProperty( prop ) ) props.push( prop );
}
return props;
}
});
Armed with this, you can sort your object using a custom sort function:
var obj = {"TR":{"fans":1848,"country_name":"Turkey"}/*...*/}
var sortedKeys = obj.ownProps().sort(function(a,b){
return obj[a].fans - obj[b].fans;
});