-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!

7
  • 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? Commented Jul 24, 2012 at 11:39
  • And why you are not sorting array first and then put sorted array into object Commented Jul 24, 2012 at 11:40
  • possible duplicate of Sorting JavaScript Object by property value Commented Jul 24, 2012 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? Commented Jul 24, 2012 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. Commented Jul 24, 2012 at 11:46

3 Answers 3

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]; ... } 
Sign up to request clarification or add additional context in comments.

3 Comments

I also was thinking about it. Thanks!
And ideas how to sort this array of objects by producers?
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.

4 Comments

@maverix: you did ask the question.
thanks, I didn't know that it's also matter of implementation... :/
@maverix: yes, such an array could be sorted, like Bergi suggests.
@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.
-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);​ ​ 

3 Comments

The question had been edited whilst I was working on a sample!!
@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.
Sorry... I was just mad because 10th person write "sort before" when i wrote that i can't... :/

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.