/** * Static Helper functions to sort events * * @param Event $a * @param Event $b * @return int */ static function CompareDate(&$a, &$b) { $adate = $a->getDate(); $bdate = $b->getDate(); //-- non-dated events should sort according to the preferred sort order if (is_null($adate) && !is_null($a->sortDate)) { $ret = $a->sortOrder - $b->sortOrder; } else { if (is_null($bdate) && !is_null($b->sortDate)) { $ret = $a->sortOrder - $b->sortOrder; } else { $ret = GedcomDate::Compare($adate, $bdate); } } if ($ret == 0) { $ret = $a->sortOrder - $b->sortOrder; //-- if dates are the same they should be ordered by their fact type if ($ret == 0) { $ret = Event::CompareType($a, $b); } } // print "[".$a->getTag().":".$adate->isOK().":".$adate->MinJD()."-".$adate->MaxJD()." ".$b->getTag().":".$bdate->isOK().":".$bdate->MinJD()."-".$bdate->MaxJD()." ".$ret."] "; return $ret; }
/** * 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 unknown_type $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->getValue("DATE") == NULL) { $nondated[] = $event; } else { $dated[] = $event; } } //-- sort each type of array usort($dated, array("Event", "CompareDate")); usort($nondated, array("Event", "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 && Event::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++; } }