Exemple #1
0
/**
 * A multi-key sort
 * 1. First divide the facts into two arrays one set with dates and one set without dates
 * 2. Sort each of the two new arrays, the date using the compare date function, the non-dated
 * using the compare type function
 * 3. Then merge the arrays back into the original array using the compare type function
 *
 * @param WT_Fact[] $arr
 */
function sort_facts(&$arr)
{
    $dated = array();
    $nondated = array();
    //-- split the array into dated and non-dated arrays
    $order = 0;
    foreach ($arr as $event) {
        $event->sortOrder = $order;
        $order++;
        if ($event->getDate()->isOk()) {
            $dated[] = $event;
        } else {
            $nondated[] = $event;
        }
    }
    //-- sort each type of array
    usort($dated, array("WT_Fact", "CompareDate"));
    usort($nondated, array("WT_Fact", "CompareType"));
    //-- merge the arrays back together comparing by Facts
    $dc = count($dated);
    $nc = count($nondated);
    $i = 0;
    $j = 0;
    $k = 0;
    // while there is anything in the dated array continue merging
    while ($i < $dc) {
        // compare each fact by type to merge them in order
        if ($j < $nc && WT_Fact::CompareType($dated[$i], $nondated[$j]) > 0) {
            $arr[$k] = $nondated[$j];
            $j++;
        } else {
            $arr[$k] = $dated[$i];
            $i++;
        }
        $k++;
    }
    // get anything that might be left in the nondated array
    while ($j < $nc) {
        $arr[$k] = $nondated[$j];
        $j++;
        $k++;
    }
}