1

Possible Duplicate:
Sorting a JavaScript object
Sort JavaScript object by key

I have array:

var arr = {}
arr[2323] = 1
arr[123] = 1
...
arr[n+232323] = 1

How to get all element of aobject sorted by key ( number order ? )

for ( key in arr ) {
 alert(typeof(key))
}

return string type.

asked Aug 3, 2012 at 14:17
3
  • 2
    you mean object because there is nothing called associative array in javascript Commented Aug 3, 2012 at 14:19
  • I am a bit bothered by the fact that people are answering saying you cannot sort these, clearly if all the keys are numeric they can be sorted. Maybe he desires to iterate through the keys (in numeric order) so he can load them into a new object where the keys aren't quite as far apart. Commented Aug 3, 2012 at 14:42
  • Objects cannot be directly sorted, however "am not i am"'s answer will provide an ordered list of keynames within the object, so they can be called in secession. Commented Aug 3, 2012 at 14:44

4 Answers 4

1

This is not an assosiative array, this is an object. There are no associative arrays in javascript.

Additionally, objects are not ordered. The order of keys in an object is meaningless.

answered Aug 3, 2012 at 14:21
1

Assuming there's some reason you don't use an Array in the first place, you can get an Array of the enumerable object properties, and sort that Array...

var sorted = Object.keys(my_obj)
 .sort(function(a,b) {
 return a - b;
 });

This assumes the keys are numeric.

Then you can iterate the Array, and use each key to get the value from my_obj...

sorted.forEach(function(key) {
 console.log(my_obj[key]);
 });
answered Aug 3, 2012 at 14:22
11
  • keys are by definition strings in this language. So you either should parseInt or use +keyname or call .localeCompare on the a or b string. Commented Aug 3, 2012 at 14:24
  • 1
    @jAndy: "numeric" is different than "number". You can have numeric strings. I'm just saying that my sorting is assuming that a numeric sort is desired. Commented Aug 3, 2012 at 14:24
  • Note that this will return an ordered array listing the keynames of the object that you want to access. Commented Aug 3, 2012 at 14:26
  • 1
    of course you can, but it's bad karma I think. You shouldn't leave it up the black magic to convert stuff correctly. Commented Aug 3, 2012 at 14:26
  • 2
    @amnotiam: yea I totally agree with Object.keys and .sort(), I'd do the same. Just that (hopefully castable) - (hopefully castable) annoyed me a little, because the simple solution for the problem :p Commented Aug 3, 2012 at 14:33
1

Short answer: You can't.

Long answer: Associative Arrays in JavaScript are really JavaScript objects. When you add a new element, you're really adding a new member to the object. While most browsers will enumerate those members in the order they were added, the standard states that the order is undefined. You can't sort something that is undefined.

answered Aug 3, 2012 at 14:23
1

JavaScript objects (maps/dictionaries/associative arrays) have no order, you can't sort them. You will need to convert it to an array first. As you only need the keys of your object in your loop, the Object.keys() function (potentionally needs a shim for older browsers) is destined for the task:

var obj = {...};
var keys = Object.keys(obj).sort(function(a,b){return a-b;}); // numerically sorted
for (var i=0; i<keys.length; i++) {
 alert(keys[i]);
 // access the values by obj[keys[i]]
}
answered Aug 3, 2012 at 14:23

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.