0

I want to create a directory list using this array example and I wanna sort it to 2 column sort, filename ASC and type DESC. Currently I have this directory sorted to filename ASC only using javascript.

Does someone have better idea how to do it? Php array sort or javascript.

Thanks a lot for your help!

Array
(
[Music] => Array
    (
        [size] => 4096
        [mode] => 16877
        [type] => 2
        [atime] => 1503581347
        [mtime] => 1504775242
        [filename] => Music
    )
[Videos] => Array
    (
        [size] => 4096
        [mode] => 16877
        [type] => 2
        [atime] => 1501249251
        [mtime] => 1502733819
        [filename] => Videos
    )
[Desktop] => Array
    (
        [size] => 4096
        [mode] => 16877
        [type] => 2
        [atime] => 1501249251
        [mtime] => 1502733819
        [filename] => Desktop
    )
[file.zip] => Array
    (
        [size] => 5084312
        [mode] => 33188
        [type] => 1
        [atime] => 1504773615
        [mtime] => 1504773620
        [filename] => file.zip
    )

Expected Output :

FILENAME | TYPE
Desktop  |  2
Music    |  2
Videos   |  2
file.zip |  1
  • You can create your own function by referring these answers, for JS :- https://stackoverflow.com/questions/28560801/javascript-sorting-array-by-multiple-criteria and for PHP :- https://stackoverflow.com/questions/3606156/sort-an-associative-array-in-php-with-multiple-condition – Lalit Sep 07 '17 at 11:25
  • Look at your options for sorting an array with php - http://php.net/manual/de/array.sorting.php – Oliver Sep 07 '17 at 11:30
  • Do it with JS and not with PHP! --> mostly you do not have to care about your clients performance. Normally JS is fast enough. But with PHP you are stacking server performance: Think of a scenario where X thousand clients want to get that sorted! – inetphantom Sep 07 '17 at 11:45

3 Answers3

1

Assuming your array is called $a, this should work.

foreach ($a as $key => $row) {
    $type[$key] = $row['type'];
}

array_multisort($type, SORT_DESC, $a);

var_dump($a);
0

If you imagine that you could live with a PHP Solution, why not try using PHP's uksort() Function? Here's how:

GIVEN ARRAY:

<?php

    $arr = array
    (
        'Music' => array
        (
            'size' => 4096,
            'mode' => 16877,
            'type' => 2,
            'atime' => 1503581347,
            'mtime' => 1504775242,
            'filename' => 'Music',
        ),
        'Videos' => array
        (
            'size' => 4096,
            'mode' => 16877,
            'type' => 2,
            'atime' => 1501249251,
            'mtime' => 1502733819,
            'filename' => 'Videos',
        ),
        'Desktop' => array
        (
            'size' => 4096,
            'mode' => 16877,
            'type' => 2,
            'atime' => 1501249251,
            'mtime' => 1502733819,
            'filename' => 'Desktop',
        ),
        'file.zip' => array
        (
            'size' => 5084312,
            'mode' => 33188,
            'type' => 1,
            'atime' => 1504773615,
            'mtime' => 1504773620,
            'filename' => 'file.zip',
        )
    );  

SIMPLE CALLABLE COMPARISON FUNCTION:

<?php

    function compare($a, $b){
        return $a > $b;
    }


    uksort($arr, 'compare');
    var_dump($arr);

QUICK-TEST HERE

Poiz
  • 7,611
  • 2
  • 15
  • 17
  • what is `$sss`? Where do yo say that you sort by type? – inetphantom Sep 07 '17 at 11:48
  • @inetphantom One would much appreciate a proactive approach that directly points out something that seems odd rather than this obsolete **WHAT IS** and **WHERE...** questions that only add more complexity to an otherwise obvious simplicity.... and by the way: Don't learn to write like Mr. A or B... a little Authenticity would be much more creative & ***pro-INETPHANTOM*** .... – Poiz Sep 07 '17 at 11:52
0

If you use the PHP side you can sort by a key/value pair array; however if you want to use Javascript you have to create a routine that will sort on an object instead of array because JS does not have Arrays that use the key/value paradigm.

So to use JS you will need something like: $sss = { 'Music': { ... (edited to remove extraneous text)