Campustream 1.0
A social network MQP for WPI
|
00001 <? 00002 00003 class sortedarray { 00004 private $data; 00005 00006 public function __construct() { 00007 00008 } 00009 00010 public function add($ele, $sorter) { 00011 if (count($this->data) === 0) { 00012 $this->data = array($ele); 00013 return $this; 00014 } 00015 00016 if ($sorter($this->data[0], $ele) > 0) { 00017 array_unshift($this->data, $ele); 00018 return $this; 00019 } elseif ($sorter($this->data[count($this->data)-1], $ele) < 0) { 00020 array_push($this->data, $ele); 00021 return $this; 00022 } 00023 00024 for ($i = 1; $i < count($this->data); $i++) { 00025 if (!$this->data[$i]) { break; } 00026 $result = $sorter($this->data[$i], $ele); 00027 if ($result < 0) { 00028 continue; 00029 } elseif ($result === 0) { 00030 // insert after this element 00031 array_splice($this->data, $i, 0, $ele); 00032 } else { 00033 // insert before this element 00034 array_splice($this->data, $i-1, 0, $ele); 00035 } 00036 } 00037 00038 return $this; 00039 } 00040 00041 public function result() { 00042 return $this->data; 00043 } 00044 00045 }