I would like to sort an array of version strings in php.
Input would be something like this:
["2019.1.1.0", "2019.2.3.0", "2019.2.11.0", "2020.1.0.0", "2019.1.3.0", "2019.3.0.0"]
What is the easiest way to sort this? Sorting the strings as is does not work, because that would put the “2019.2.11.0” before “2019.2.3.0” and that is of course not what I want.
Result should be
["2019.1.1.0", "2019.1.3.0", "2019.2.3.0", "2019.2.11.0", "2019.3.0.0", "2020.1.0.0"]
2
Answers
You are searching for a natural sort.
You should use
version_compare
function.This is how to use
usort
with version_compare.Result