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.

7
  • 5
    You can't sort an object. You can sort arrays, however. Object properties have no intrinsic ordering. Commented May 1, 2015 at 21:30
  • 2
    Here are some tips: stackoverflow.com/questions/1069666/… Commented May 1, 2015 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 stackoverflow.com/questions/5467129/… Commented May 1, 2015 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. Commented May 1, 2015 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. Commented May 1, 2015 at 22:15

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.