Sort folders on top except when sorted by "Modified"

I list folders and files separately. I would like folders to be listed on top except in the case where I'm sorting a list by "modified". In other words, once a lister is sorted by "modified" (whether manually or as a saved default) the folders should group at the bottom. Is it possible to do this?

I don't think this can be directly set in the settings (except in Folder Formats), but you could use a button that combines the two commands and/or have a Script Add-in do it after a folder change.

Set SORTBY=modified
Set SORTORDER=files

(See Raw Commands in How to use buttons and scripts from this forum)

// FoldersDownIfSortedByModified

// This is a script for Directory Opus.
// See https://www.gpsoft.com.au/DScripts/redirect.asp?page=scripts for development information.

// Called by Directory Opus to initialize the script
function OnInit(initData) {
    initData.name = 'FoldersDownIfSortedByModified';
    initData.default_enable = true;
    initData.min_version = '12.0';
}

// Called after a new folder is read in a tab
function OnAfterFolderChange(afterFolderChangeData) {
    var cmd = DOpus.Create.Command;
    cmd.SetSourceTab(afterFolderChangeData.tab);
    if (afterFolderChangeData.tab.format.sort_field == 'modified') {
        cmd.RunCommand('Set SORTORDER=files');
    } else {
        cmd.RunCommand('Set SORTORDER=folders');
    }
}

FoldersDownIfSortedByModified.js.txt (726 Bytes)

(Download, then drag the file to the list in Preferences / Toolbars / Scripts.)

1 Like

(I've added one line running cmd.SetSourceTab ... to the script above which should help when folders open in the background. Edited in the post to keep things tidy.)

Going back to the button before the script, you could make it into a toggle like this:

@if:Set SORTBY=modified
Set SORTBY=name
Set SORTORDER=folders
@if:else
Set SORTBY=modified
Set SORTORDER=files

If you click it when sorting by anything other than Modified, it will switch to showing files before folders, and sorting by modified.

If you click it when already sorting by Modified, it will switch to showing folders before files and sorting by name.

1 Like

Thanks for the detailed guidance! It works.

1 Like

When I use this script, sometimes when sorting by name it sorts Z to A rather than A to Z. Is there any way to make it always sort A to Z?

Add SORTREVERSE=Off:

@if:Set SORTBY=modified
Set SORTBY=name SORTREVERSE=Off
Set SORTORDER=folders
@if:else
Set SORTBY=modified
Set SORTORDER=files

Thanks. I did look in the manual but I was unable to find that! Just FYI, the description of sortreverse in the manual is pretty general, which may be because sorting can have a lot of different parameters, but reading it I couldn't tell how it would sort the column "date modified" based on the description – I had to try it on and off to see which way was going to start with the most recent modified and end with the oldest. (For any other readers, "off" has newest-modified at the top).

I tried this, using the script. However, now Folders are always at the bottom, even when sorting by name. Is there a way to get the specified behavior of the OP:

which is also what I would like? :slight_smile:
Maybe something changed in dopus in the last two years?

(BTW, being a newbie with dopus I hadn't realized at first that it's necessary to Exit Directory Opus and restart to activate the newly-added script. Just mentioning it here for the benefit of the next newbie who reads this thread.)

That isn't necessary. Just copy the script into the scripts folder, or drag it on to the scripts list in Preferences, and the script will be installed.

Some scripts may only affect new windows, but that's up to the script, and still doesn't require you exit and restart the whole program.

I don't think there is a way for a script to detect changes in the sort column/order (no event that fires). The OnAfterFolderChange event we used in the script gets triggered "after a new folder has been read in a tab".

So can either use the script and hit refresh after clicking the column header, or use a button (maybe with a hotkey) from above.