-1

I have this array and I was wondering if I could sort it with the properties comments.count and likes.count separately.

For example I could just have a function to call using likes.count parameter or comments.count parameter.

{
    "attribution": null,
    "tags": [

    ],
    "type": "",
    "location": null,
    "comments": {
        "count": 2,
        "data": [
            {
                "created_time": "1385670850",
                "text": "Holy shit",
                "from": {
                    "username": "someone",
                    "profile_picture": "http://url.com/profiles/profile_191120775_75sq_1365830292.jpg",
                    "id": "191120775"
                },
                "id": "599372997379438683"
            },
            {
                "created_time": "1385680581",
                "text": "",
                "from": {
                    "username": "someone",
                    "profile_picture": "http://url.com/profiles/profile_1523167_75sq_1389544912.jpg",
                    "id": "1523167"
                },
                "id": "599454624038205610"
            }
        ]
    },
    "likes": {
        "count": 6,
        "data": [
            {
                "username": "someone",
                "profile_picture": "http://url.com/profiles/profile_2169761_75sq_1389075971.jpg",
                "id": "2169761"
            },
            {
                "username": "rashmityagi",
                "profile_picture": "http://url.com/profiles/profile_393770264_75sq_1388911033.jpg",
                "id": "393770264"
            },
            {
                "username": "tylerferweda",
                "profile_picture": "http://url.com/profiles/profile_191120775_75sq_1365830292.jpg",
                "id": "191120775"
            },
            {
                "username": "cbolts",
                "profile_picture": "http://url.com/profiles/profile_1523167_75sq_1389544912.jpg",
                "id": "1523167"
            }
        ]
    }
},
{
    "attribution": null,
    "tags": [

    ],
    "type": "",
    "location": null,
    "comments": {
        "count": 10,
        "data": [
            {
                "created_time": "1385670850",
                "text": "Holy shit",
                "from": {
                    "username": "someone",
                    "profile_picture": "http://url.com/profiles/profile_191120775_75sq_1365830292.jpg",
                    "id": "191120775"
                },
                "id": "599372997379438683"
            },
            {
                "created_time": "1385680581",
                "text": "",
                "from": {
                    "username": "someone",
                    "profile_picture": "http://url.com/profiles/profile_1523167_75sq_1389544912.jpg",
                    "id": "1523167"
                },
                "id": "599454624038205610"
            }
        ]
    },
    "likes": {
        "count": 20,
        "data": [
            {
                "username": "someone",
                "profile_picture": "http://url.com/profiles/profile_2169761_75sq_1389075971.jpg",
                "id": "2169761"
            },
            {
                "username": "rashmityagi",
                "profile_picture": "http://url.com/profiles/profile_393770264_75sq_1388911033.jpg",
                "id": "393770264"
            },
            {
                "username": "tylerferweda",
                "profile_picture": "http://url.com/profiles/profile_191120775_75sq_1365830292.jpg",
                "id": "191120775"
            },
            {
                "username": "cbolts",
                "profile_picture": "http://url.com/profiles/profile_1523167_75sq_1389544912.jpg",
                "id": "1523167"
            }
        ]
    }
},
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
basic1point0
  • 321
  • 1
  • 2
  • 7
  • Also note that your question has **nothing** to do with JSON. After the JSON is parsed, you are dealing with regular JS objects and arrays, – Felix Kling Jan 24 '14 at 04:41

2 Answers2

0

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort

json_array.sort(function(a, b) {
    if(a.comments.count < b.comments.count) {
        return -1;
    }
    if(a.comments.count > b.comments.count) {
        return 1;
    }
    return 0;
});

You can also modify it for likes.count

Zero Fiber
  • 4,417
  • 2
  • 23
  • 34
0

Underscore.js provides great utility functions for working with arrays and objects. For your case you could use _.sortBy(...) See http://underscorejs.org/#sortBy for more details. For simple object properties it is enough to specify the object property name als last parameter.