let’s say I have such weird code:
var weirdo = {};
var ids = [10, 30, 11, 1, 4, 2];
var producers = ['Ford','Rover','BMW','Fiat','Renault','Mitsubishi'];
var n = producers.length;
for(var i=0; i<n; i++) {
weirdo[ids[i]] = producers[i];
}
// unknown code ???
How to sort weirdo by values? It’s not array and no, I can’t sort producers before filling weirdo.
Any ideas?
Oh, and relation id <=> producer is VERY IMPORANT!
You can use an array of objects:
Then you can sort that array by id:
…and iterate it with a for-loop.
Another solution would be an iterator array, to loop over an id<->producer object (
weirdo) in the correct order: