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
3
  • You can create your own function by referring these answers, for JS :- stackoverflow.com/questions/28560801/… and for PHP :- stackoverflow.com/questions/3606156/… Commented Sep 7, 2017 at 11:25
  • Look at your options for sorting an array with php - php.net/manual/de/array.sorting.php Commented Sep 7, 2017 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! Commented Sep 7, 2017 at 11:45

3 Answers 3

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);
Sign up to request clarification or add additional context in comments.

Comments

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

2 Comments

what is $sss? Where do yo say that you sort by type?
@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 ....
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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.