# Custom Column - Example custom sort

**URL:** https://resource.dopus.com/t/custom-column-example-custom-sort/18738
**Category:** Buttons/Scripts
**Tags:** script, column, jscript
**Created:** [July 14, 2014, 9:16pm UTC](https://resource.dopus.com/t/custom-column-example-custom-sort/18738 "2014-07-14T21:16:54Z")
**Posts on this page:** 1
**Showing post:** 1

<div class="post-metadata">

### Author: ![wowbagger](https://resource.dopus.com/user_avatar/resource.dopus.com/wowbagger/32/25880_2.png) [@wowbagger](https://resource.dopus.com/u/wowbagger)
#### Post date: [July 14, 2014, 9:16pm UTC](https://resource.dopus.com/t/custom-column-example-custom-sort/18738/1 "2014-07-14T21:16:54Z")

</div>

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](https://resource.dopus.com/t/extreme-custom-sort/11781/1).

> [@Julianon](#):
>
> I want the extension-groups sorted in the order:  
> pdf, txt, tex, dvi, log, ptp

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.

```javascript
// 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.

---

_[View the full topic](https://resource.dopus.com/t/custom-column-example-custom-sort/18738)._
