Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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!

share|improve this question
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 –  Juhana 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

3 Answers 3

up vote 5 down vote accepted

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];
    ...
}
share|improve this answer
    
I also was thinking about it. Thanks! –  marverix Jul 24 '12 at 12:11
    
And ideas how to sort this array of objects by producers? –  marverix Jul 24 '12 at 12:12
2  
@marverix: stackoverflow.com/questions/5421253/… –  Felix Kling Jul 24 '12 at 12:14
    
Thanks Felix! :) –  marverix Jul 24 '12 at 12:15

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.

share|improve this answer
    
Jesus I know... -.-" How many times I must say this... –  marverix Jul 24 '12 at 12:03
    
@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
    
so better way is array of objects? like [{'id':1,'producer':'Ford'}, .... ] ? –  marverix Jul 24 '12 at 12:09
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

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);​
​
share|improve this answer
    
Read carefully question... –  marverix Jul 24 '12 at 12:04
    
The question had been edited whilst I was working on a sample!! –  Gavin Jul 24 '12 at 12:06
2  
@maverix: you could do with some manners. Keep in mind that people are here to help you and have no obligation whatsoever to do so. –  David Hedlund Jul 24 '12 at 12:08
    
Sorry... I was just mad because 10th person write "sort before" when i wrote that i can't... :/ –  marverix Jul 24 '12 at 12:14

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.