What is Nostr?
mx amber alex /
npub1672…vvam
2024-09-29 20:25:48

mx amber alex on Nostr: jfc, JavaScript… I've got an array of objects. Each object has a date property. ...

jfc, JavaScript…

I've got an array of objects. Each object has a date property. These objects will be displayed on a timeline, and to avoid them overlapping, they are arranged in a stairs-like order: each subsequent element is arranged higher or lower than the previous one.

To facilitate this, the entire array is sorted, via a custom sorting function that compares the date property of all objects. It's pretty simple:

timeline.events.sort(compareEvents);

...

function compareEvents(first, second) {
if(first.date == second.date) { return 0; }
if(first.date > second.date) { return 1; }
if(first.date < second.date) { return -1; }

return 0;
}


Pretty trivial, right? If the dates are equal (no such cases currently exist in the array), return 0; else return -1 or +1 depending on which one is earlier / later. All by the book.

But when I run this, the whole thing is sorted in some absurd manner, where every so often, an object is at the ENTIRELY wrong place in the array, leading to two subsequent objects on the timeline being at the same height and overlapping, because they are at entirely different places in the array.

What the fuck?

#JavaScript #AskFedi
Author Public Key
npub1672pskw9uttatp6ynpt4ppzl05yntczyc72hagecghq45lmh3zsq0pvvam