[ create a new paste ] login | about

Link: http://codepad.org/OsWlhVpV    [ raw code | output | fork ]

PHP, pasted on Feb 18:
<?php
class Item
{
    public $p;

    public function __construct($p)
    {
        $this->p = $p;
    }
}

function cmp($a, $b)
{
    if ($a->p == $b->p) {
        return 0;
    }

    return ($a->p < $b->p) ? -1 : 1;
}

$items[] = new Item(5.0);
$items[] = new Item(3.0);
$items[] = new Item(-1.0);
$items[] = new Item(2.5);

usort($items, "cmp"); // sort itemsay of Items using `cmp` function

foreach ($items as $x) {
    echo "{$x->p}\n";
}


Output:
1
2
3
4
-1
2.5
3
5


Create a new paste based on this one


Comments: