FANDOM


This is a call back PHP function that you can use for sorting version numbers through usort(), uasort(), and uksort().

    /**
     * 1.0.1 > 1.0.0
     * 1.0.1 < 1.0.10
     * 1.0.1 = 1.0.1.0 (cast nulls to zero?)
     * 1.0.1 = 1.0.1.0.0.0
     * 1.0.1 < 1.0.1.0.1
     * 1.0.1 < 1.0.1.1
     *
     * @author Simon Cornelius P. Umacob rot13<fvzbapch@tznvy.pbz>
     */
    function version_sort($a, $b)
    {
        $a_list = explode('.', $a);
        $b_list = explode('.', $b);

        $max = count($a_list) > count($b_list) ? count(a_list) : count($b_list);

        for ($i = 0; $i < $max; $i++) {
           if ((int) $a_list[$i] > (int) $b_list[$i]) {
               return 1;
           } else if ((int) $a_list[$i] < (int) $b_list[$i]) {
               return -1;
           }
        }

        return 0;
    }