I've searched StackOverflow before I made this question but I can't find the solutions to my case.
I'm receiving objects from multiple Facebook Graph API calls (see first image).
What I would like to do is to sort my objects by a date (event.start_time).
Is this possible with or without creating an extra array?
The structure of the objects is visible in the second picture.
the variable "event" holds all the objects.
UPDATE I made a global array and pushed all the event objects to it, it's now an array so it should be sortable. I just don't know how to, please help.
var arr = []; //global array to sort event objects
//function is called somewhere
function makeEvents(ids, infowindow, map, accessToken) {
var currentTime = dateFormat("date", new Date(), true);
//console.log(currentTime);
var display = document.getElementById("all");
var carousel = document.getElementById("carousel-inner");
// console.log(ids);
FB.api('/',
{ids : ids},
function (pages) {
if(pages) {
$.each(pages, function(page_key, page_value) {
var id = page_value.id;
//display.innerHTML += '<tr>';
//console.log(id);
//if date search changed, since date search input
FB.api('/'+ id +'/events?access_token='+ accessToken +'&since='+ currentTime, function (events) { //&offset=0 &until=2016-02-31
//console.log(events.data);
if(events) {
//console.log(events.data)
$.each(events.data, function(events_key, events_value) {
FB.api('/'+ events_value.id + '?fields=id,name,cover,description,start_time,place,ticket_uri,picture', function (event) {
if(event) {
//console.log(event);
arr.push(event);//fill array
}
});
});
}
});
// display.innerHTML += '</tr>';
});
}
});
}
console.log(arr);
arr.sort(function(a, b){
if(a.start_time < b.start_time)
return -1;
if(a.start_time > b.start_time)
return 1;
return 0;
});
This shouldn't work since I'm not deep enough in the array.
SOLVED Had to use a timeout function because the array sorted before it was completely filled which made me think it didn't sort at all. If there's a better way to check if the array is done filling, please let me know :)
setTimeout(function(){
arr = arr.sort(function (a, b) {
return a.name.localeCompare( b.name );
});
console.log(arr);
}, 5000);
