dracoblue.net

PHP script to order builds, release numbers and versions

I had the problem, that I wanted to sort an array of builds by name, but names could be 1.1 or, 2.10.0.1 or even 2.0.9.

Thatswhy I made the following sort alogrithm in PHP by using usort and an own compare function.

function builds_sort($a, $b) {
    if ($a['name']==$b['name']) {
        return 0;
    }
    $ap=explode('.',$a['name']);
    $bp=explode('.',$b['name']);
    foreach ($ap as $pos => $number) {
        if (!isset($bp[$pos])) {
          return 1;
        } elseif ($number > $bp[$pos]) {
          return 1;
        } elseif ($number < $bp[$pos]) {
          return -1;
        }
    }
    return -1;
}

// Testarray
$builds=array(
    array("name"=>"2.1.0","id"=>"210","suite_id"=>1),
    array("name"=>"2.1.1","id"=>"211","suite_id"=>1),
    array("name"=>"3.0.0","id"=>"3","suite_id"=>1),
    array("name"=>"10","id"=>"3","suite_id"=>1),
    array("name"=>"2.1.10","id"=>"2110","suite_id"=>1),
    array("name"=>"2.1.2","id"=>"212","suite_id"=>1)
);

usort ($builds,"builds_sort");

This function sorts each element by "name" with DESC.

In open source, php by @ 06 Nov 2007

comments powered by Disqus

Recent Files

Advertisement

Recent Dev-Articles

Read recently

About

Blogroll