Last active
          February 10, 2018 16:49 
        
      - 
      
 - 
        
Save arvindsvt/5a06dc78419da352c82fcd50ec29666d to your computer and use it in GitHub Desktop.  
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | <?php | |
| $myArray = array(1, 19, 18, 12, 56); | |
| function compare($a, $b) { | |
| echo "Comparing $a to $b\n"; | |
| if ($a == $b) return 0; | |
| return ($a < $b) ? -1 : 1; | |
| } | |
| usort($myArray,"compare"); | |
| print_r($myArray); | |
| ?> | |
| <?php | |
| function cmp ($a, $b) | |
| { | |
| global $w_o; | |
| if ($a[$w_o] == $b[$w_o]) return 0; | |
| return ($a[$w_o] < $b[$w_o]) ? -1 : 1; | |
| } | |
| # the index is the second element of | |
| # each row | |
| $w_o =1; | |
| usort($my_arry_info,"cmp"); | |
| ?> | |
| this is a new multisort function for sorting on multiple subfield like it will be in sql : 'ORDER BY field1, field2' | |
| number of sort field is undefined | |
| <?php | |
| $array[] = array('soc' => 3, 'code'=>1); | |
| $array[] = array('soc' => 2, 'code'=>1); | |
| $array[] = array('soc' => 1, 'code'=>1); | |
| $array[] = array('soc' => 1, 'code'=>1); | |
| $array[] = array('soc' => 2, 'code'=>5); | |
| $array[] = array('soc' => 1, 'code'=>2); | |
| $array[] = array('soc' => 3, 'code'=>2); | |
| //usage | |
| print_r(multiSort($array, 'soc', 'code')); | |
| function multiSort() { | |
| //get args of the function | |
| $args = func_get_args(); | |
| $c = count($args); | |
| if ($c < 2) { | |
| return false; | |
| } | |
| //get the array to sort | |
| $array = array_splice($args, 0, 1); | |
| $array = $array[0]; | |
| //sort with an anoymous function using args | |
| usort($array, function($a, $b) use($args) { | |
| $i = 0; | |
| $c = count($args); | |
| $cmp = 0; | |
| while($cmp == 0 && $i < $c) | |
| { | |
| $cmp = strcmp($a[ $args[ $i ] ], $b[ $args[ $i ] ]); | |
| $i++; | |
| } | |
| return $cmp; | |
| }); | |
| return $array; | |
| } | |
| ?> | |
| output: | |
| Array | |
| ( | |
| [0] => Array | |
| ( | |
| [soc] => 1 | |
| [code] => 1 | |
| ) | |
| [1] => Array | |
| ( | |
| [soc] => 1 | |
| [code] => 1 | |
| ) | |
| [2] => Array | |
| ( | |
| [soc] => 1 | |
| [code] => 2 | |
| ) | |
| [3] => Array | |
| ( | |
| [soc] => 2 | |
| [code] => 1 | |
| ) | |
| [4] => Array | |
| ( | |
| [soc] => 2 | |
| [code] => 5 | |
| ) | |
| [5] => Array | |
| ( | |
| [soc] => 3 | |
| [code] => 1 | |
| ) | |
| [6] => Array | |
| ( | |
| [soc] => 3 | |
| [code] => 2 | |
| ) | |
| ) | |
| <?php | |
| $listSort[] = array('email' => '[email protected]', 'name' => 'x', 'umur' => 21); | |
| $listSort[] = array('email' => '[email protected]', 'name' => 'y', 'umur' => 11); | |
| $listSort[] = array('email' => '[email protected]', 'name' => 'z', 'umur' => 33); | |
| foreach ($listSort as $key => $row) { | |
| $name2[$key] = $row['name']; | |
| $umur2[$key] = $row['umur']; | |
| } | |
| array_multisort($umur2, SORT_DESC, $name2, SORT_ASC, $listSort); | |
| echo '<pre>'; | |
| echo print_r($listSort); | |
| echo '</pre>'; | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment