简体   繁体   中英

How to sort versioning info

What's the right way, to handle versioning indicators like 2.4 or 2.4.0.9 etc. to get the ability of sorting versions.

PHP says, that 1.3.4 is not a valid integer, but also a non-valid number.

array('2.4','2.3.4','2.4.0.9')

PHP has a version_compare function. Use usort to sort it. Like following. :)

$a = array('2.4','2.3.4','2.4.0.9');
usort($a, 'version_compare');

Or, just use natsort :

$array = array('2.4','2.16.6','2.3.4','2.4.0.9');
natsort($array);
print_r($array);

#Array ( [2] => 2.3.4 [0] => 2.4 [3] => 2.4.0.9 [1] => 2.16.6 )

Storing it as a string allows you to make use of the version_compare() function:

$versions = array('2.4','2.3.4','2.4.0.9');
usort($versions, 'version_compare');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM