Skip to content
Advertisement

Natural array sorting with proper decimal support in PHP

I want to sort an array with numbers in natural order, so numbers with a bigger value come after smaller ones like this:

JavaScript

This is what I get as result, which is not the result I need, decimals are not threated correctly.

JavaScript

There is already a similar question Array Sorting in php for decimal values but no fitting answer was found there.

The expected output for a property sorting would be

JavaScript

So I would kind of expect natsort() do to exactly that, but it looks like it is buggy and I have to implement a similar logic by my self? Is that correct?

One solution I am thinking of is to reformat the numbers somehow to fixed precision and hope that natsort() works then, but I am wondering if there are easier solutions or PHP-builtin ones.

I tried https://github.com/awssat/numbered-string-order which is very interesting but also does not support decimals.

Advertisement

Answer

I’m not 100% sure of your specification so please test this, but strnatcmp seems like it can be used to run a natsort variant in usort. If both strings passed to the comparator start with float numbers, then cast them to floats and use the spaceship, otherwise, default to strnatcmp.

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement