How to sort versioning info

前端 未结 3 1312
误落风尘
误落风尘 2020-12-30 01:39

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

相关标签:
3条回答
  • 2020-12-30 01:52

    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 )
    
    0 讨论(0)
  • 2020-12-30 02:00

    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');
    
    0 讨论(0)
  • 2020-12-30 02:03

    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');
    
    0 讨论(0)
提交回复
热议问题