No hay una forma concisa de manipular directamente las "claves" de un objeto Javascript. Realmente no está diseñado para eso. ¿Tiene la libertad de poner sus datos en algo mejor que un objeto normal (o una matriz, como sugiere su código de muestra)?
Si es así, y si su pregunta podría reformularse como "¿Qué objeto similar a un diccionario debería usar si quiero iterar sobre las claves en orden ordenado?" entonces podrías desarrollar un objeto como este:
var a = {
keys : new Array(),
hash : new Object(),
set : function(key, value) {
if (typeof(this.hash[key]) == "undefined") { this.keys.push(key); }
this.hash[key] = value;
},
get : function(key) {
return this.hash[key];
},
getSortedKeys : function() {
this.keys.sort();
return this.keys;
}
};
// sample use
a.set('b',1);
a.set('z',1);
a.set('a',1);
var sortedKeys = a.getSortedKeys();
for (var i in sortedKeys) { print(sortedKeys[i]); }
Si no tiene control sobre el hecho de que los datos están en un objeto regular, esta utilidad convertiría el objeto regular en su diccionario completamente funcional:
a.importObject = function(object) {
for (var i in object) { this.set(i, object); }
};
Esta fue una definición de objeto (en lugar de una función de constructor reutilizable) por simplicidad; editar a voluntad.