A Fun Sorting Lexicographically Bug

Ran into a fun little bug today. We have an integration that’s been around since 2016 where we save their id values as a string because we can’t rely on them not throwing in letters. Looking at the db it seems like they were earlier on but have been only using numbers for a while.
They just went over 100 million (they probably started at 90 million or something like that since we were okay up until 100 million) on their id values. Can you guess what happened?

Here’s your hint, we don’t cast to an int.

All of the new records coming into our system and being sorted by their id are being displayed at the end of the lists!
Yup, because we are sorting numbers lexicographically up until 99999999 values we were good on sorting, but when you sort lexicographically 10000000 comes before 999999999.

I’m thinking we’ll need to use PHP’s is_numeric and then cast all the values coming out of our db to an int when that is true and raise the alarm if it’s ever false.

So, we’ve apparently been kicking this can down the road for 6 years and our time has come due!

Leave a Reply

Your email address will not be published. Required fields are marked *