The Data Was Correct Until It Was Sorted
The numbers didn’t look wrong at first.
The API returned the right dataset, the counts matched expectations, and nothing was missing. But once the data hit the UI, something felt off. Items that should have been at the top were buried somewhere in the middle, and the ordering changed depending on how often the page refreshed.
It wasn’t random, but it wasn’t predictable either.
My first assumption was that the backend wasn’t enforcing a sort properly. I checked the query and confirmed it was sorting by a timestamp field. Then I logged the raw response before it hit the frontend. The order was correct.
So the issue had to be happening after that.
I suspected the frontend next. Maybe a state update was reordering things unintentionally. I walked through the rendering logic, but nothing stood out. The array was being passed directly into a sort function with a comparator that looked straightforward.
Still, the output didn’t match the input.
I pulled both the backend response and the frontend sorting logic into Blackbox AI and used it to trace how the data was being transformed step by step. Instead of looking at the sort in isolation, I followed the full lifecycle from API response to rendered list.
That’s when something subtle came up.
The timestamp field being used for sorting wasn’t consistent in format. Some entries were ISO strings, others were already parsed Date objects. The comparator function assumed everything was the same type, so under certain conditions, it was comparing strings lexicographically instead of comparing actual time values.
That explained why the order looked “almost right” but occasionally wrong.
I had seen the data structure before, but I hadn’t questioned the consistency of the field itself.
Using iterative edits in Blackbox AI, I normalized the timestamp field before sorting so everything was converted into a consistent numeric value. Then I reran the same transformations through the agent.
This time, the order stayed correct no matter how many times the component re-rendered.
Nothing was wrong with the sort function.
It was doing exactly what it was told. Just not with the data it expected.