We are about to switch to a new forum software. Until then we have removed the registration on this forum.
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
to format code, edit your post, highlight the code bits and press ctrl-o.
This might help:
...and see also:
Thanks for your attention. Actually what i want to do is not to sort the array by several properties at the same time, but to sort the array by a first property, e.g. the "name" and log to see the result, which should be the objects in order of "name" A,B,C.... and then sort the array by another property, e.g. the "id" and the result should be in the order of "id" 0,1,2... But the result as you can see in the chrome log output is the same, which is the result of the last call of the sort() function. I dont understand why....Or did I miss something?
@charles1990 - your code is almost certainly working exactly as you intend! The problem is that you're logging a reference to the object to the console...
Try logging the stringified object instead:
console.log(JSON.stringify(data));
Since you're logging a reference the console 'helpfully' shows you the current (i.e. final) state of the object and not the state at the point you logged it...
Thank you so much!! I also tried the print() in p5.js, which also worked as my intension....and also thank you for your explanation of how console.log() works....