Laravel-filemanager: Sort by time default

Created on 6 Jun 2017  路  7Comments  路  Source: UniSharp/laravel-filemanager

I need load files order by "time DESC" when the iframe of laravel-filemanager is called.
Is posible? I read the code and see that we cant order by time DESC and the code dont have options to configure a default "sort_type"

enhancement

Most helpful comment

To load files order by "time DESC"
you can change the code in vendor/unisharp/laravel-filemanager/src/traits/LfmHelpers.php

   public function sortFilesAndDirectories($arr_items, $sort_type)
    {
        if ($sort_type == 'time') {
            $key_to_sort = 'updated';
        } elseif ($sort_type == 'alphabetic') {
            $key_to_sort = 'name';
        } else {
            $key_to_sort = 'updated';
        }


           return strcmp($a->{$key_to_sort}, $b->{$key_to_sort});
        });

        return $arr_items;
    }

with

   public function sortFilesAndDirectories($arr_items, $sort_type)
    {
        if ($sort_type == 'time') {
            $key_to_sort = 'updated';
        } elseif ($sort_type == 'alphabetic') {
            $key_to_sort = 'name';
        } else {
            $key_to_sort = 'updated';
        }

        uasort($arr_items, function ($a, $b) use ($key_to_sort) {
            if ( $a->$key_to_sort == $b->$key_to_sort )
                return 0;
            else if ( $a->$key_to_sort > $b->$key_to_sort)
                return -1;
            else
                return 1;
        });

        return $arr_items;
    }

All 7 comments

Maybe we should add a config to set default sort type.

Thanks g0110280,
When do you think that the changes will be made?

where in the config file that I can change/modify the sorting, bro?

To load files order by "time DESC"
you can change the code in vendor/unisharp/laravel-filemanager/src/traits/LfmHelpers.php

   public function sortFilesAndDirectories($arr_items, $sort_type)
    {
        if ($sort_type == 'time') {
            $key_to_sort = 'updated';
        } elseif ($sort_type == 'alphabetic') {
            $key_to_sort = 'name';
        } else {
            $key_to_sort = 'updated';
        }


           return strcmp($a->{$key_to_sort}, $b->{$key_to_sort});
        });

        return $arr_items;
    }

with

   public function sortFilesAndDirectories($arr_items, $sort_type)
    {
        if ($sort_type == 'time') {
            $key_to_sort = 'updated';
        } elseif ($sort_type == 'alphabetic') {
            $key_to_sort = 'name';
        } else {
            $key_to_sort = 'updated';
        }

        uasort($arr_items, function ($a, $b) use ($key_to_sort) {
            if ( $a->$key_to_sort == $b->$key_to_sort )
                return 0;
            else if ( $a->$key_to_sort > $b->$key_to_sort)
                return -1;
            else
                return 1;
        });

        return $arr_items;
    }

Thanks @anasmorahhib, your code help me a lot, but I also need to change the default sort_type var in vendor/unisharp/laravel-filemanager/public/js/script.js
I have replaced this line:
var sort_type = 'alphabetic';
for this:
var sort_type = 'updated';

Is there a way I can order by "time DESC" without changing the code in vendor/unisharp/laravel-filemanager/src/traits/LfmHelpers.php?

This is my solution
File Vendor/Unisharp/laravel-filemanager/public/js/script.js
row 3
instead var sort_type = 'alphabetic';
var sort_type = 'time';

File Vendor/Unisharp/laravel-filemanager/src/Controllers/ItemsController.php
instead
'items' => array_map(function ($item) {
return $item->fill()->attributes;
}, array_merge($this->lfm->folders(), $this->lfm->files())),
change to
'items' => array_map(function ($item) {
return $item->fill()->attributes;
}, array_merge($this->lfm->folders(), array_reverse($this->lfm->files()))),

Sorry for my english

Was this page helpful?
0 / 5 - 0 ratings

Related issues

lunadiotic picture lunadiotic  路  3Comments

intergalactisch picture intergalactisch  路  5Comments

farshidahmadianpin picture farshidahmadianpin  路  3Comments

DaxtonChen picture DaxtonChen  路  4Comments

fahadhowlader picture fahadhowlader  路  5Comments