-2

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!

marverix
  • 7,184
  • 6
  • 38
  • 50
  • 3
    You cannot sort object properties. If you want a specific order, you have to use an array. I wonder why you need `weirdo` to be an object at all, if you have numerical keys anyways. Why don't you just create a copy of `producers`? – Felix Kling Jul 24 '12 at 11:39
  • And why you are not sorting array first and then put sorted array into object – SergeS Jul 24 '12 at 11:40
  • possible duplicate of [Sorting JavaScript Object by property value](http://stackoverflow.com/questions/1069666/sorting-javascript-object-by-property-value) – JJJ Jul 24 '12 at 11:40
  • In straight way no, but there must be any way to do that - for example if I wonna after this write for( in ) loop then? – marverix Jul 24 '12 at 11:41
  • 1
    @marverix: If you could provide more information about what you are trying to do (the overall problem/scenario), what you want to do with `weirdo`, why (you think) it must be an object, etc., then we could help you better. Otherwise the answer is that you can't do it. – Felix Kling Jul 24 '12 at 11:46
  • Weirdo is "array" (i know that it isn't) where key is ID and value is title for this ID. I need to iterate this array but first i need to sort this "array" by values. So I can't use answer from here: http://stackoverflow.com/questions/1069666/sorting-javascript-object-by-property-value because I need IDs (in this answer conversion to array and to object is good point, but not in my problem). – marverix Jul 24 '12 at 11:53
  • Then you have to create an other array which only contains IDs, sort it and then iterate over your object (`weirdo`) in the order of the IDs in that array. *edit:* ah, you added an `ids` array... – Felix Kling Jul 24 '12 at 12:02

3 Answers3

5

You can use an array of objects:

var items = [
  {id:10, producer:'Ford'},
  {id:30, producer:'Rover'},
  {id:11, producer:'BMW'},
  {id:1, producer:'Fiat'},
  {id:4, producer:'Renault'},
  {id:2, producer:'Mitsubishi'},
]

// or, starting from your two arrays:
var items = [];
for (var i=0; i<ids.length && i<producers.length; i++)
    items[i] = {id:ids[i], producer:producers[i]};

Then you can sort that array by id:

items.sort(function(a, b){ return a.id-b.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:

var weirdo = {"10":"Ford","30":"Rover",...};
var ids = Object.keys(weirdo); // if not already built somewhere

ids.sort(function(a,b){return a-b;}); // sort numeric
// loop:
for (var i=0; i<ids.length; i++) {
    var id=ids[i], producer=weirdo[id];
    ...
}
Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
2

The short answer is that objects are unordered by spec. The properties of an object are just that - properties. They are served as you request them, they are not items to be moved around within the object.

How an object is visualized upon inspection is entirely up to the agent performing the visualization. For instance, if you were to write the following in Chrome

console.log({ x: 1, a: 2 });

... it would display the parameters in alphabetical order. That is not something innate to the object, but simply a matter of chrome implementation.

If you would, instead, write the following in chrome

for(key in { x: 1, a: 2 }) console.log(key);

... it would log an x, followed by an a, i.e. showing the properties for the same object in the order that they were added.

You could of course make sure that you add the properties to your object in the alphabetical/numerical order of their respective property names, but that would be missing the point, as there is no guarantee that all implementations will preserve the order of adding either.

David Hedlund
  • 128,221
  • 31
  • 203
  • 222
  • @maverix: you did ask the question. – David Hedlund Jul 24 '12 at 12:03
  • thanks, I didn't know that it's also matter of implementation... :/ – marverix Jul 24 '12 at 12:08
  • @maverix: yes, such an array could be sorted, like Bergi suggests. – David Hedlund Jul 24 '12 at 12:10
  • 1
    @marverix: It could have been so much easier if you had explained your **actual** problem and asked for a solution, instead of asking for a solution to your solution. If you simply ask how to sort properties, the answer is "you can't" and trying to hold on to that does not get you anywhere. – Felix Kling Jul 24 '12 at 12:12
-1

Have you tried using Array.Sort?

http://jsfiddle.net/gRoberts/mXwdN/

var weirdo = {};
var producers = ['Ford','Rover','BMW','Fiat','Renault','Mitsubishi'];
producers.sort();
console.log(producers);
producers.map(function(item, index) {
    this[index] = item;
}, weirdo);
console.log(weirdo);

Edit: Due to question being updated whilst I was working on a solution, please find an updated version based on the updated question:

http://jsfiddle.net/gRoberts/mXwdN/1/

var weirdo = {};
var ids = [10, 30, 11, 1, 4, 2];
var producers = ['Ford','Rover','BMW','Fiat','Renault','Mitsubishi'];
ids.sort();
ids.map(function(item, index) {
    weirdo[item] = producers[index];
}, weirdo);
console.log(weirdo);​
​
Gavin
  • 6,284
  • 5
  • 30
  • 38