2

I started to use javascript / jquery a few weeks ago, and I'm already stucked. I have an array / object with this stucture:



    {
        "Array1": [
        {
            "data1": "value",
            "data2": "value"
        },
        {
            "data1": "value",
            "data2": "value"
        }
        ],
        "Array2": [
        {
            "data1": "value",
            "data2": "value"
        },
        {
            "data1": "value",
            "data2": "value"
        }
        ]
    }

I'd like to sort this array by Array1 and Array2. I've tried to use .sort() multiple way but it always says: "the object doesn't support this property or method". Is there any way to do this sorting? Could you write me some examples? Thank you very much.

iuwan
  • 43
  • 4
  • 5
    You can't sort an object. You can sort arrays, however. Object properties have no intrinsic ordering. – Pointy May 01 '15 at 21:30
  • Oh. I didn't know that. Thank you for the information! – iuwan May 01 '15 at 21:33
  • 2
    Here are some tips: http://stackoverflow.com/questions/1069666/sorting-javascript-object-by-property-value – Mackan May 01 '15 at 21:41
  • 1
    @Pointy You can't sort an object but you can sort the keys and then recreate the object from ssorted key list or you can add keys in a specific order gived by the value sorting. Please see more here http://stackoverflow.com/questions/5467129/sort-javascript-object-by-key – vasilenicusor May 01 '15 at 22:06
  • 1
    @vasilenicusor yes, but there is no guaranteed ordering of object properties. A JavaScript implementation is free to return object keys in any order it wants, even through repeated calls to `Object.keys()` or in a `for ... in` loop. Relying on property ordering is an extremely bad idea. – Pointy May 01 '15 at 22:11
  • @Pointy During last 8 years i don't get any fail with this tricks. Officially you cannot do this, but unofficially work. – vasilenicusor May 01 '15 at 22:15
  • @vasilenicusor you implement your website code, I'll do mine :) You never know when some genius at Google will find a new Chrome optimsation that causes your code to fail. If it's not in the spec, then relying on the behavior is an obvious (and, really, pointless) risk. – Pointy May 01 '15 at 22:16
  • @Pointy you're right – vasilenicusor May 01 '15 at 22:22

1 Answers1

1

Although you can't sort an object, you can use a loop, a total, string concatenation, and bracket-style notation to access properties in a desired order. For instance, in your example, if you added a property that indicated how many data points you had, you could use its value to build up a loop:

var myArray = {
    "Array1": [
    {
        "data1": "value",
        "data2": "value"
    },
    {
        "data1": "value",
        "data2": "value"
    }
    ],
    "Array2": [
    {
        "data1": "value",
        "data2": "value"
    },
    {
        "data1": "value",
        "data2": "value"
    }
    ],
    arrayCount:2
}

for (n=1; n<=myArray.arrayCount; n++) {
    console.log(myArray["Array" + n]);
}

This would result in the same effect as:

console.log(myArray["Array1"]);
console.log(myArray["Array2"]);

I believe this would be your best bet in accessing data in an object in a specific order.

Eric N
  • 1,856
  • 11
  • 13