-3

I have an Array like the following, how would I sort it by the keys of the objects it contains?

var myArray = [
    item : {

    },
    anotherItem : {

    }
];

EDIT

myArray is an Object not an Array. Does that change anything?

Nope
  • 22,147
  • 7
  • 47
  • 72
Nathan McCallum
  • 237
  • 1
  • 4
  • 18
  • 1
    I don't think that's valid... =/ – jeremy Mar 31 '13 at 01:39
  • 3
    This is not valid [Javascript Array](http://www.w3schools.com/js/js_obj_array.asp) syntax. – Aiias Mar 31 '13 at 01:39
  • 1
    possible duplicate of [How to sort an array of javascript objects?](http://stackoverflow.com/questions/979256/how-to-sort-an-array-of-javascript-objects) or [Sorting objects in an array by a field value in JavaScript](http://stackoverflow.com/questions/1129216/sorting-objects-in-an-array-by-a-field-value-in-javascript) – Nope Mar 31 '13 at 01:43
  • `myArray is an Object not an Array - does that change anything?` I'm afraid it is neither and is simply invalid syntax. Trying to run your code as is [**in jsFiddle**](http://jsfiddle.net/HHcMD/) for example generates `Uncaught SyntaxError: Unexpected token : ` You can see the error in the debugger's console log when running the fiddle. – Nope Mar 31 '13 at 02:09

2 Answers2

2

In Javascript, arrays can only be declared with numeric keys and objects can be declared with alphanumeric keys (attributes).

It may help to take a look at Javascript's array sort() method.

However, although there is not a way to declare an array with non-numeric keys, as icktoofay has pointed out, since arrays are a special type of object, it is possible to set non-numeric keys for arrays after they have been declared.

Aiias
  • 4,683
  • 1
  • 18
  • 34
  • As an array is an object, it actually *can* have non-numeric keys, but there is no special syntax for them as there is in plain object literals and it is not treated specially by the array. – icktoofay Mar 31 '13 at 01:45
  • @icktoofay - Thanks for pointing out those details about arrays. – Aiias Mar 31 '13 at 01:51
  • Sorry guys, see my edit. – Nathan McCallum Mar 31 '13 at 02:05
  • @TheVasari - Take a look at some related questions: http://stackoverflow.com/questions/5467129/sort-javascript-object-by-key , http://stackoverflow.com/questions/2947822/javascript-sort-objects , and http://stackoverflow.com/questions/1069666/sorting-javascript-object-by-property-value . – Aiias Mar 31 '13 at 02:08
  • @Aiias, thank you, that first link was exactly what I needed! – Nathan McCallum Mar 31 '13 at 02:17
0

First of all, that syntax is invalid: there is no way to set a key in an array literal. Additionally, while it is possible, you probably do not want an array with non-numeric properties; arrays are designed to hold numeric properties.

Even if those were corrected, properties in an object have no defined iteration order:

The mechanics and order of enumerating the properties […] is not specified.

Sorting, by definition, affects the order; if there is no defined order in the first place, you cannot really change it.

icktoofay
  • 126,289
  • 21
  • 250
  • 231