I am facing a challenge in ordering an array based on a nested object. The array contains information about objects on a timeline and I would like to sort it by the start
position defined within nested arrays. Currently, I am able to iterate through the array using lines[0].events[0].start
Below is the structure of the array:
timelines = [
{
name: 'obj1',
data: { id : 'obj1-guid' },
lines: [{
events: [{
name: 'animation1',
data : { id : 'animation1-guid' },
start : 100,
duration : 200
}]
}],
video_url: 'url',
},
{
name: 'obj2',
data: { id : 'obj2-guid' },
lines: [{
events: [{
name: 'animation1',
data : { id : 'animation1-guid' },
start : 4,
duration : 200
}]
}],
video_url: 'url',
},
{
name: 'obj3',
data: { id : 'obj3-guid' },
lines: [{
events: [{
name: 'animation1',
data : { id : 'animation1-guid' },
start : 56,
duration : 200
}]
}],
video_url: 'url',
},
];
I have attempted to write a sorting function as follows:
function sorting(json_object, key_to_sort_by) {
function sortByKey(a, b) {
var x = a[key_to_sort_by];
var y = b[key_to_sort_by];
return ((x < y) ? -1 : ((x > y) ? 1 : 0));
}
json_object.sort(sortByKey);
}
timelines = sorting(timelines, 'lines[0].events[0].start');
However, this function is not functioning as expected.