php - uasort bad performance sorting an array of object with custom order -
i have sort array of objects called "contents", made this:
"articles" => array:106 [▼ 0 => array:5 [▼ "id" => 467823568 "title" => "my tittle" "data" => "my data" "category" => 23 "order" => 2 ] 1 => array:5 [▼ "id" => 46782356433 "title" => "my tittle 2" "data" => "my data 2" "category" => 25 "order" => 1 ] ... ]
the order defined in 2 array, category_o
, order_o
made this:
"category_o" => array:21 [▼ 0 => 25 1 => 95 2 => 135 3 => 72 4 => 4 5 => 23 6 => 7 7 => 803 ... ]
i have articles sorted property "category" custom order specified in category_o
array, , second sort property "order" other custom order specified in order_o
.
to sort articles use uasort
method:
// call sort function uasort($articles, array($this, "sortbycategoryorder"));
with custom function sortbycategoryorder($left, $right);
:
function sortbycategoryorder($leftitem, $rightitem){ // array contains order $order = $this->category_o; // exchange key value, can access "position value" $flipped = array_flip($order); // init default value position $rightpos = 0; $leftpos = 0; // check if order of current category present: // $category_o array not have order // values of articles if ( (array_key_exists($leftitem["category"], $flipped)) && (array_key_exists($rightitem["category"], $flipped)) ){ // no missing: 2 element have order $leftpos = $flipped[$leftitem["category"]]; $rightpos = $flipped[$rightitem["category"]]; }else if (array_key_exists($leftitem["category"], $flipped)) { // miss right: right elmenet has not custom order specified! $leftpos = $flipped[$leftitem["category"]]; $rightpos = $leftpos+1; }else if (array_key_exists($rightitem["category"], $flipped)){ // miss left: left elmenet has not custom order specified! $rightpos = $flipped[$rightitem["category"]]; $leftpos = $rightpos+1; }else{ // miss left , right: 2 articles have no custom order specified! $rightpos = 99999; $leftpos = 99999; } // make comparation return $leftpos >= $rightpos; }
i have second function called sortbyorder($left, $right);
sort articles order_o
array values.
it takes around 12 seconds sort ca. 2000 articles. uasort
realy slow or have made mistake in custom order functions? thanks
the solution:
i've found right solution @ahmad hajjar.
i wrote method set each content new property category_o
, order_o
, use double natural ascending order php's method array_multisort()
. performance increase , current 2000 articles pass from ca. 9 secs ca. 1 secs!
i report new method, hope hopeful:
public function sortarticles($articles, $type) { // init sorted $sorted = array(); // array contains order $order_c = $this->category_o; // exchange key value, can access "position value" $flipped_c = array_flip($order_c); // array contains order $order_o = $this->order_o; // exchange key value, can access "position value" $flipped_o = array_flip($order_o); // iterate each article foreach ($articles $article) { // if custom order specified set article // otherwise, set 99999 value. if ( array_key_exists( $article["category"], $flipped_c) ){ $article["category_o"] = $flipped_c[$article["category"]]; }else{ $article["category_o"] = 99999; } // if custom order specified set article // otherwise, set 99999 value. if ( array_key_exists($article["order"], $flipped_o) ){ $article["order_o"] = $flipped_o[$article["order"]]; }else{ $article["order_o"] = 99999; } // push new article inside array array_push($sorted, $article); } // list of sort columns , data pass array_multisort $sort = array(); foreach($sorted $key => $value) { $sort['order_o'][$key] = $value['order_o']; $sort['category_o'][$key] = $value['category_o']; } // determinate order type, category or order if (strcmp($type, "category") == 0) { // sort category_o asc , order_o asc array_multisort($sort['category_o'], sort_asc, $sort['order_o'], sort_asc,$sorted); }else{ // sort order_o asc , category_o asc array_multisort($sort['order_o'], sort_asc, $sort['category_o'], sort_asc,$sorted); } // return sorted array return $sorted; }
i think model design issue. go changing model store category_o ranks along category values , order_o ranks along values, them db ordered whatever need ... when array db
"articles" => array:106 [▼ 0 => array:5 [▼ "id" => 467823568 "title" => "my tittle" "data" => "my data" "category" => 23 "category_o" => 5 "order" => 2 "order_o" => 1 ] 1 => array:5 [▼ "id" => 46782356433 "title" => "my tittle 2" "data" => "my data 2" "category" => 25 "category_o" => 0 "order" => 1 "order_o" => 2 ] ... ]
Comments
Post a Comment