Custom Column - Example custom sort

This is an example of how to use the Dopus 11.5.1 and higher to create a custom column to allow custom sorting.

As requested by Julianon in Extreme custom sort.

In the code below, we define an array (config.sortOrder) that contains a list of the file extensions, and the sort value for that extension. Multiple columns can be configured.

// The OnInit function is called by Directory Opus to initialize the script add-in
function OnInit(initData) {
  // Provide basic information about the script by initializing the properties of the ScriptInitData object
  initData.name = "Custom Sort Column";
  initData.desc = "Adds that allows extension sorting in a custom order";
  initData.copyright = "2014 wowbagger";
  initData.min_version = "11.5.1"
  initData.version = "1.1";
  initData.default_enable = true;

  
  // Create a new ScriptColumn object and initialize it to add the column to Opus
  for(var i = 0; i < config.columns.length; ++i) {
    var cmd = initData.AddColumn();
    cmd.name = config.columns[i].name;
    cmd.method = "OnCustomSort";
    cmd.label = config.columns[i].name;
    cmd.autogroup = true;       
    cmd.justify = "left";
  }
}

var config = {
  columns: [{
    name: "CustomSort", //Sample sort config 1
    sortOrder: [{
        extension:"pdf",
        sortKey: "1"
      }, {
        extension:"txt",
        sortKey: "2"
      }, {
        extension:"tex",
        sortKey: "3"
      }, {
        extension:"dvi",
        sortKey: "4"
      }, {
        extension:"log",
        sortKey: "7"
      }, {
        extension:"ptp",
        sortKey: "8"
      }]
    }, {
    name: "CustomSort2", //Sample sort config 2
    sortOrder: [{
        extension:"txt",
        sortKey: "1"
      }, {
        extension:"",
        sortKey: "1.5"
      }, {
        extension:"log",
        sortKey: "2"
      }]
   }],
  debug: false
}
 
// Implement the OnCustomSort column (this entry point is an OnScriptColumn event).
function OnCustomSort(scriptColData) {
    var columnConfig = GetConfig(scriptColData.col);
    if(!columnConfig) return;
    
    var ext = scriptColData.item.ext.replace(".","").toLowerCase();
    var sortOrder = GetSortOrder(columnConfig, ext);
    if (!sortOrder) { 
      scriptColData.value = config.debug ? "0:nomatch" : "";
      scriptColData.sort = 0;
      return;
    }
    else
    {
      scriptColData.value = config.debug ? sortOrder.sortKey + ":" + sortOrder.extension : sortOrder.extension;
      scriptColData.sort = sortOrder.sortKey;
    }
}

function GetSortOrder(columnConfig, extension)
{
  var match = null;
  
  for(var i = 0; i < columnConfig.sortOrder.length; ++i) {
    if(columnConfig.sortOrder[i].extension.toLowerCase() == extension) {
      match = columnConfig.sortOrder[i];
      break;
    }
  }
  return match;
}

function GetConfig(key)
{
  var match = null;
   
  for(var i = 0; i < config.columns.length; ++i) {
    if(config.columns[i].name == key) {
      match = config.columns[i];
      break;
    }
  }
 
 return match;
}

How to use
Place the code (.vbs or .js) or .osp file into your Script AddIns folder (/dopusdata/Script AddIns).
You will then find the "CustomSort" column in Folder Options in the new Script category.

If you create your own custom sort scrips please share.

1 Like

The filenames are in the format "somealphatext nnnnn_xxx", where I want to sort by "xxx".

function OnInit(initData){
  initData.name = "PnNumber";
  initData.desc = "Add a sortable column for pn number";
  initData.copyright = "myarmor";
  initData.version = "1.0";
  initData.default_enable = false;

  var col = initData.AddColumn();
  col.name = "Number";
  col.method = "OnNumber";
  col.label = "Number";
  col.justify = "left";
  col.autogroup = false;
  col.autorefresh=true;
}

// Implement the OnNumber column
function OnNumber(scriptColData){
  if (scriptColData.col!="Number") return;
  if (scriptColData.item.is_dir) {
    scriptColData.value = "";
    scriptColData.sort = 3;
  } else{
    var re=/^\w+ \d+[_-](\d+)/i;
    if (re.exec(scriptColData.item.name)){
      scriptColData.value = RegExp.$1;
    } else{
      scriptColData.value = "No Number";
    }
  }
}

Save the script with a .js extension into the addins folder.
Use at your own risk and whatnot, but feel free to adapt.

1 Like

Answer to Sorting files by filename part.
Creates the columns Col1, Col2, and Col3. Allows independent sorting of each numeric part of the filename.

function OnInit(initData)
{
  initData.name = "MultiColSort";
  initData.desc = "Add sortable columns for numeric parts";
  initData.copyright = "";
  initData.version = "1.0";
  initData.default_enable = false;

  var col = initData.AddColumn();
  col.name = "Col1";
  col.method = "OnCol1";
  col.label = "Col1";
  col.justify = "left";
  col.autogroup = false;
  col.autorefresh=true;

  col = initData.AddColumn();
  col.name = "Col2";
  col.method = "OnCol2";
  col.label = "Col2";
  col.justify = "left";
  col.autogroup = false;
  col.autorefresh=true;

  col = initData.AddColumn();
  col.name = "Col3";
  col.method = "OnCol3";
  col.label = "Col3";
  col.justify = "left";
  col.autogroup = false;
  col.autorefresh=true;
}

function ColCommon(colnumber,scriptColData)
{
  if (scriptColData.item.is_dir) {
    scriptColData.value = "";
    scriptColData.sort = 3;
  } else{
    var re=/^.+_(\d+)_(\d+)_(\d+)/i;
    if (re.exec(scriptColData.item.name)){
      switch (colnumber){
        case 1: scriptColData.value = RegExp.$1;break;
        case 2: scriptColData.value = RegExp.$2;break;
        case 3: scriptColData.value = RegExp.$3;break;
      }
    } else{
      scriptColData.value = "";
    }
  }
}

// Implement the OnCol1 column
function OnCol1(scriptColData)
{
  if (scriptColData.col!="Col1") return;
  ColCommon(1,scriptColData);
}
// Implement the OnCol2 column
function OnCol2(scriptColData)
{
  if (scriptColData.col!="Col2") return;
  ColCommon(2,scriptColData);
}

// Implement the OnCol3 column
function OnCol3(scriptColData)
{
  if (scriptColData.col!="Col3") return;
  ColCommon(3,scriptColData);
}

Hi @wowbagger!
I would like to ask, if you have a tiny amount of time, how should i approach your script (where you can sort by extension) to display folders like in the original EXT Column, like <dir> or, with any custom string? Is it possible? I made some modifications, i have added a huge list of extensions sorted by your awesome custom column, but i would like it to display Folders more distinctively, and not by being empty?

Edit: Never mind! I did it somehow! I do not fully understand JS yet, im only creating VB macros in Excel at my workplace, but it seems i figured out the syntax :smiley:
Your script is very useful for me!

1 Like

Nice one, Be sure to share your solution. Someone else might like it.

Yep, i will post it, had to get off my desk for some drinks, then back to configuring Opus :slight_smile:

So here is my version, not fully finished, i will implement a lot more extensions to sort by.

I added extensionout: to allow for a custom string for the extension if needed, also for directories, "<DIR>" can be changed to any character string to make directories pop out for better visuals.

I also modified the sorting numbers to minus, for example the EXE to -1.1 so it sorts as the first entry, instead the first after unspecified ones.

I am still learning the script syntax so if anyone has any suggestion, or simplifications, i would appreciate it.

// The OnInit function is called by Directory Opus to initialize the script add-in
function OnInit(initData) {
  // Provide basic information about the script by initializing the properties of the ScriptInitData object
  initData.name = "Custom Sort Column";
  initData.desc = "Extension sorting in a custom order";
  initData.copyright = "2014 wowbagger";
  initData.min_version = "12"
  initData.version = "1.2";
  initData.default_enable = true;
  // Create a new ScriptColumn object and initialize it to add the column to Opus
  for(var i = 0; i < config.columns.length; ++i) {
    var cmd = initData.AddColumn();
    cmd.name = config.columns[i].name;
    cmd.method = "OnCustomSort";
    cmd.label = config.columns[i].name;
    cmd.autogroup = true;       
    cmd.justify = "left";
  }
}
var config = {
  columns: [{
    name: "ext", //Sample sort config 1
    sortOrder: [{
        extension:"",
		extensionout:"",
		sortKey: "-100"
      }, {
        extension:"exe",
        extensionout:"exe",
		sortKey: "-1.1"
      }, {
        extension:"lnk",
        extensionout:"lnk",
		sortKey: "-1.11"
      }, {
        extension:"bat",
        extensionout:"bat",
		sortKey: "-1.2"
      }, {
        extension:"com",
        extensionout:"com",
		sortKey: "-1.2"
      }, {
        extension:"msi",
        extensionout:"msi",
		sortKey: "-1.2"
      }, {
        extension:"mp3",
        extensionout:"mp3",
		sortKey: "-2.0"
      }, {
        extension:"wav",
        extensionout:"wav",
		sortKey: "-2.1"
      }, {
        extension:"m4a",
        extensionout:"m4a",
		sortKey: "-2.2"
      }, {
        extension:"wma",
        extensionout:"wma",
		sortKey: "-2.3"
      }, {
        extension:"aup",
        extensionout:"aup",
		sortKey: "-2.8"
      }, {
        extension:"ptx",
        extensionout:"ptx",
		sortKey: "-2.9"
      }, {
        extension:"mp4",
        extensionout:"mp4",
		sortKey: "-3.0"
      }, {
        extension:"avi",
        extensionout:"avi",
		sortKey: "-3.1"
      }, {
        extension:"mpg",
        extensionout:"mpg",
		sortKey: "-3.2"
      }, {
        extension:"mkv",
        extensionout:"mkv",
		sortKey: "-3.3"
      }, {
        extension:"ogv",
        extensionout:"ogv",
		sortKey: "-3.4"
      }, {
        extension:"vob",
        extensionout:"vob",
		sortKey: "-3.5"
      }, {
        extension:"wmv",
        extensionout:"wmv",
		sortKey: "-3.6"
      }, {
        extension:"flv",
        extensionout:"flv",
		sortKey: "-3.7"
      }, {
        extension:"bmp",
        extensionout:"bmp",
		sortKey: "-4.0"
      }, {
        extension:"jpg",
        extensionout:"jpg",
		sortKey: "-4.1"
      }, {
        extension:"gif",
        extensionout:"gif",
		sortKey: "-4.2"
      }, {
        extension:"png",
        extensionout:"png",
		sortKey: "-4.3"
      }, {
        extension:"zip",
        extensionout:"zip",
		sortKey: "-5"
      }, {
        extension:"7z",
        extensionout:"7z",
		sortKey: "-5.1"
      }, {
        extension:"rar",
        extensionout:"rar",
		sortKey: "-5.2"
      }, {
        extension:"txt",
		extensionout:"txt",
		sortKey: "-6"
      }, {
        extension:"doc",
        extensionout:"doc",
		sortKey: "-6.1"
      }, {
        extension:"pdf",
        extensionout:"pdf",
		sortKey: "-6.2"
      }, {
        extension:"xml",
        extensionout:"xml",
		sortKey: "-6.3"
      }, {
        extension:"xls",
        extensionout:"xls",
		sortKey: "-6.4"
      }, {
        extension:"log",
        extensionout:"log",
		sortKey: "-7"
      }, {
        extension:"log",
        extensionout:"log",
		sortKey: "-8"
      }]
    }],
  debug: false
}
// Implement the OnCustomSort column (this entry point is an OnScriptColumn event).
function OnCustomSort(scriptColData) {
    var columnConfig = GetConfig(scriptColData.col);
    if(!columnConfig) return;
    var ext = scriptColData.item.ext.replace(".","").toLowerCase();
    var sortOrder = GetSortOrder(columnConfig, ext);
    if (!sortOrder) { 
      scriptColData.value = config.debug ? "0:nomatch" : "? "+ext;
      scriptColData.sort = 0;
      return;
    }
    
    else if (scriptColData.item.is_dir)
    {
      scriptColData.value = "<DIR>";
    }

    else
    {
      scriptColData.value = config.debug ? sortOrder.sortKey + ":" + sortOrder.extension : sortOrder.extensionout;
      scriptColData.sort = sortOrder.sortKey;
    }
}
function GetSortOrder(columnConfig, extension)
{
  var match = null;
  for(var i = 0; i < columnConfig.sortOrder.length; ++i) {
    if(columnConfig.sortOrder[i].extension.toLowerCase() == extension) {
      match = columnConfig.sortOrder[i];
      break;
    }
  }
  return match;
}
function GetConfig(key)
{
  var match = null;
  for(var i = 0; i < config.columns.length; ++i) {
    if(config.columns[i].name == key) {
      match = config.columns[i];
      break;
    }
  }
 return match;
}

Edit: Oh and it also displays the unspecified extensions by putting a "?" in front of them but they are not sorted (only by filename)