PHP in the array of elements to sort, this is very often used, the previous project also has, and for several sorts we are used is Asort arsort and other PHP native functions, not to implement, so on a few functions to summarize, this will continue to complement, Oneself can also review and summarize well.
/* Insert sort (one-dimensional array) * each time a data element to be sorted is inserted into the appropriate position in the previously sequenced sequence, the sequence remains orderly until all the data elements to be sorted are inserted. */function Insertsort ($arr) {if (!is_array ($arr) | | Count ($arr) ==0) {return $arr; } $count =count ($arr); for ($i =1; $i < $count; $i + +) {if (Isset ($arr [$i])) {$tmp = $arr [$i];//Gets the value of the latter element $j = $i -1;//Gets the preceding subscript while ($arr [$j] > $tmp) {//If the front one is larger than the next one, this is from small to large $arr [$j +1] = $arr [$j];//the small element and the front of the swap until it moves to the appropriate position in the move $arr [$j] = $tmp; $j--; }}} return $arr;} /* Select sort (one-dimensional array) * Each trip selects the smallest (largest) element from the data element to be sorted, placing the order at the end of the ordered sequence until all the data elements to be sorted are exhausted. */function Selectsort ($arr) {if (!is_array ($arr) | | Count ($arr) = = 0) {return $arr; } $count =count ($arr); for ($i =0; $i < $count; $i + +) {$k = $i; for ($j = $i +1; $j < $count; $j + +) {if ($arr [$k] > $arr [$j]) $k = $j;//Find the smallest if ($k! = $i) {$tmp = $arr [$i]; $arr [$i] = $arr [$k]; $arr [$k] = $tmp; }}} return $arr;} /* Bubble sort (one-dimensional array) * 22 compares the size of the data element to be sorted, and finds that two data elements are exchanged in reverse order until there are no reversed data elements */function Bubblesort ($array) {$count =count ($array ); if ($count <= 0) {return false; } for ($i =0; $i < $count; $i + +) {for ($j = $count-1; $j > $i; $j-) {if ($array [$j] < $array [$j-1]) {//compare found The number of $tmp = $array [$j]; $array [$j] = $array [$j-1]; $array [$j-1] = $tmp; }}} return $array;} /* Quick sort (one-dimensional array) * */function QuickSort ($array) {if (count ($array) <= 1) {return $array; } $key = $array [0]; $left _arr =array (); $right _arr =array (); for ($i =1; $i
$val) {$arrKey [] = $key; $arrVal [] = $val; } $count =count ($arrVal); if ($count) {//Create a sequential array of key for ($key =0; $key < $count; $key + +) {$arrKeyMap [$key] = $key; }//Sort values for ($i =0; $i < $count; $i + +) {for ($j = $count-1; $j > $i; $ j--) {//
<从小到大排列 升降在这修改 $bol="$strOrder" =="'asc'" ?$arrval[$j]<$arrval[$j-1] :$arrval[$j]>
$arrVal [$j-1]; if ($bol) {$tmp = $arrVal [$j]; $arrVal [$j] = $arrVal [$j-1]; $arrVal [$j-1] = $tmp; The value of the bubble sort, causing the interaction of the array of key $keytmp = $arrKeyMap [$j]; $arrKeyMap [$j] = $arrKeyMap [$j-1]; $arrKeyMap [$j-1] = $keytmp; }}} if (count ($arrKeyMap)) {foreach ($arrKeyMap as $val) { $arrReturn [] = $arrKey [$val]; }} return $arrReturn; }}/** * Arrays are arranged by value using native functions */function arraysortbyval ($arr, $keys, $type = ' asc ') {$keysvalue = $new _array =array (); foreach ($arr as $k = + $v) {$keysvalue [$k] = $v [$keys]; } if ($type = = ' asc ') {asort ($keysvalue); }else{Arsort ($keysvalue); } reset ($keysvalue); foreach ($keysvalue as $k = + $v) {$new _array[$k] = $arr [$k]; } return $new _array;
For the following 2 methods to sort the values of the array one is to implement the original PHP function is used, in fact, the order of a small amount of data generally on a single page of data is still possible, if it involves a large number of data sorting, recommendations can be integrated into the basic MySQL class.