Properties (keys) of an object are not intrinsically ordered; you must maintain your own array of their ordering if you wish to do so.
Here is an example of how you could simplify ordering your sample object by arbitrary properties via custom sort functions:
var orderKeys = function(o, f) {
var os=[], ks=[], i;
for (i in o) {
os.push([i, o[i]]);
}
os.sort(function(a,b){return f(a[1],b[1]);});
for (i=0; i<os.length; i++) {
ks.push(os[i][0]);
}
return ks;
};
orderKeys(sample, function(a, b) {
return a.age - b.age;
}); // => ["Elem4", "Elem2", "Elem1", "Elem3"]
orderKeys(sample, function(a, b) {
return a.title.localeCompare(b.title);
}); // => ["Elem2", "Elem1", "Elem4", "Elem3"]
Once the properties are ordered as you like then you can iterate them and retrieve the corresponding values in order.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…