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 at Reddit & Hackernews

Give something back

Were my blog posts useful to you? If you want to give back, support one of these charities, too!

Report hate in social media Campact e.V. With our technology and your help, we protect the oceans from plastic waste. Gesellschaft fur Freiheitsrechte e. V. The civil eye in the mediterranean

Recent Dev-Articles

Read recently

Recent Files

About