Sort an array of object by different properties

Hi guys, I am trying to load a json into an array of objects and sort the array by properties of objects... but the result seems only affected by the last call of my sort function... I use chrome to test my code.

the json file is like this:

{"students":


[
        {
            "id": 1,
            "name": "A",
            "gender": "Male",
            "events":[
                {
                    "event_id": 0,
                    "start": "7:00",
                    "end": "8:20",
                    "event": "Study",
                    "location": "College"
                },
                {
                    "event_id": 1,
                    "start": "9:00",
                    "end": "13:20",
                    "event": "Sleep",
                    "location": "Dom"
                }]
        },

        {
            "id": 0,
            "name": "B",
            "gender": "Female",
            "events":[
                {
                    "event_id": 0,
                    "start": "7:09",
                    "end": "7:20",
                    "event": "Shopping",
                    "location": "Supermarket"
                },
                {
                    "event_id": 1,
                    "start": "19:00",
                    "end": "21:20",
                    "event": "Party",
                    "location": "Bar"
                }]
        },

        {
            "id": 2,
            "name": "C",
            "gender": "Male",
            "events":[
                {
                    "event_id": 0,
                    "start": "4:00",
                    "end": "6:21",
                    "event": "Eat",
                    "location": "Restaurant"
                },
                {
                    "event_id": 1,
                    "start": "19:08",
                    "end": "23:10",
                    "event": "Dance",
                    "location": "Home"
                },
                {
                    "event_id": 2,
                    "start": "15:34",
                    "end": "22:45",
                    "event": "Jump",
                    "location": "Street"
                }]
        }
    ]
}

and the sketch.js is like this:

var data;

function preload() {
    data = loadJSON("http://localhost/test.json");
}

function setup() {
    noLoop();
    data=data.students;
}

function draw() {
    background(200);

    data.sort(dynamicSort("name"));
    console.log(data);

    data.sort(dynamicSort("id"));
    console.log(data);
}


function dynamicSort(property) {
    var sortOrder = 1;
    if(property[0] === "-") {
        sortOrder = -1;
        property = property.substr(1);
    }
    return function (a,b) {
        var result = (a[property] < b[property]) ? -1 : (a[property] > b[property]) ? 1 : 0;
        return result * sortOrder;
    }
}

and the console output is:

sketch.js:21 
(3) [{…}, {…}, {…}]

0
:
{id: 0, name: "B", gender: "Female", events: Array(2)}

1
:
{id: 1, name: "A", gender: "Male", events: Array(2)}

2
:
{id: 2, name: "C", gender: "Male", events: Array(3)}

length
:
3
__proto__
:
Array(0)

sketch.js:27 
(3) [{…}, {…}, {…}]

0
:
{id: 0, name: "B", gender: "Female", events: Array(2)}

1
:
{id: 1, name: "A", gender: "Male", events: Array(2)}

2
:
{id: 2, name: "C", gender: "Male", events: Array(3)}

length
:
3
__proto__
:
Array(0)

So it seems that only sorting by "id" works and overwrite the result of sorting by "name"... Any idea why?

Answers

Sign In or Register to comment.