fork(3) download
  1. <?php
  2. // Relatively small array of various file types.
  3. // I want to display the results of the files array in this order.
  4. $sort_array = array('mp4','mpeg','flv','f4v','avi');
  5. $weights = array_flip($sort_array);
  6. // This is the array I would like to sort on filetype. The data is coming from Amazon simple DB...
  7. // and again a given media object will only have a small handful of files... in fact
  8. // probably no more than two or three.
  9. $files = array(
  10. array('title'=>'race day','filetype'=>'f4v','filename'=>'racing.f4v'),
  11. array('title'=>'playin checkers','filetype'=>'mp4','filename'=>'checkers.mp4'),
  12. array('title'=>'playin checkers','filetype'=>'mp3','filename'=>'checkers.mp3'),
  13. array('title'=>'playin checkers','filetype'=>'avi','filename'=>'checkers.avi'),
  14. array('title'=>'eating melons','filetype'=>'mp4','filename'=>'watermelon.mp4'),
  15. array('title'=>'eating melons (audio)','filetype'=>'mp3','filename'=>'watermelon.mp3')
  16. );
  17. usort($files, make_comparer(['filetype', SORT_ASC, function($t) use ($weights) { return $weights[$t]; }]));
  18.  
  19. print_r($files);
  20.  
  21.  
  22.  
  23. function make_comparer() {
  24. // Normalize criteria up front so that the comparer finds everything tidy
  25. $criteria = func_get_args();
  26. foreach ($criteria as $index => $criterion) {
  27. $criteria[$index] = is_array($criterion)
  28. ? array_pad($criterion, 3, null)
  29. : array($criterion, SORT_ASC, null);
  30. }
  31.  
  32. return function($first, $second) use (&$criteria) {
  33. foreach ($criteria as $criterion) {
  34. // How will we compare this round?
  35. list($column, $sortOrder, $projection) = $criterion;
  36. $sortOrder = $sortOrder === SORT_DESC ? -1 : 1;
  37.  
  38. // If a projection was defined project the values now
  39. if ($projection) {
  40. $lhs = call_user_func($projection, $first[$column]);
  41. $rhs = call_user_func($projection, $second[$column]);
  42. }
  43. else {
  44. $lhs = $first[$column];
  45. $rhs = $second[$column];
  46. }
  47.  
  48. // Do the actual comparison; do not return if equal
  49. if ($lhs < $rhs) {
  50. return -1 * $sortOrder;
  51. }
  52. else if ($lhs > $rhs) {
  53. return 1 * $sortOrder;
  54. }
  55. }
  56.  
  57. return 0; // tiebreakers exhausted, so $first == $second
  58. };
  59. }
  60.  
Success #stdin #stdout #stderr 0.01s 20520KB
stdin
Standard input is empty
stdout
Array
(
    [0] => Array
        (
            [title] => eating melons (audio)
            [filetype] => mp3
            [filename] => watermelon.mp3
        )

    [1] => Array
        (
            [title] => eating melons
            [filetype] => mp4
            [filename] => watermelon.mp4
        )

    [2] => Array
        (
            [title] => playin checkers
            [filetype] => mp3
            [filename] => checkers.mp3
        )

    [3] => Array
        (
            [title] => playin checkers
            [filetype] => mp4
            [filename] => checkers.mp4
        )

    [4] => Array
        (
            [title] => race day
            [filetype] => f4v
            [filename] => racing.f4v
        )

    [5] => Array
        (
            [title] => playin checkers
            [filetype] => avi
            [filename] => checkers.avi
        )

)
stderr
PHP Notice:  Undefined index: mp3 in /home/ru1gzD/prog.php on line 17
PHP Notice:  Undefined index: mp3 in /home/ru1gzD/prog.php on line 17
PHP Notice:  Undefined index: mp3 in /home/ru1gzD/prog.php on line 17
PHP Notice:  Undefined index: mp3 in /home/ru1gzD/prog.php on line 17
PHP Notice:  Undefined index: mp3 in /home/ru1gzD/prog.php on line 17
PHP Notice:  Undefined index: mp3 in /home/ru1gzD/prog.php on line 17
PHP Notice:  Undefined index: mp3 in /home/ru1gzD/prog.php on line 17