Skip to content
Advertisement

Sorting Dotted Numbers in JSON & PHP

In my project with PHP Inside the JSON is the data I keep. And each of these data has sequence numbers 1.1, 1.1.1, 1.1.2, 1.1.3, …, 1.1.10.

When I sorted, I noticed that 1.1.10 came after 1.1.1, whereas 1.1.9 wasn’t there yet.

Because it sorts alphabetically. And I don’t know how to deal with it!

Edit: Added sample JSON data.

JavaScript

The ordering looks like this:

JavaScript

I tried like this, but the result is clear! Please help me, I’m going crazy!

Advertisement

Answer

You need to use version_compare function inside of sort function:

JavaScript

For descending change places $b abd $a variables:

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