Advertisement
n0tmE

php arbitary sort

Jan 11th, 2017
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.42 KB | None | 0 0
  1. <?php
  2. function customSort($a, $b) {
  3.     $arrOrder = [4, 1, 3, 2];
  4.     $aIx = array_search($a[0], $arrOrder);
  5.     $bIx = array_search($b[0], $arrOrder);
  6.    
  7.     if ($aIx === false)
  8.         return 1;
  9.     elseif ($bIx === false)
  10.         return -1;
  11.     else
  12.         return ($aIx < $bIx)?-1:1;
  13. }
  14.  
  15. $arrItems = [
  16.         [1, 'First Item'],
  17.         [4, 'Fourth Item'],
  18.         [2, 'Second Item'],
  19.         [3, 'Third Item'],
  20. ];
  21.  
  22. usort($arrItems, 'customSort');
  23.  
  24. var_dump($arrItems);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement