Adding Index Column

Hello,

My goal is to have a column that sorts the items by date created, then assigns an index without the numbers adjusting when another column is sorted.

Though the code is incomplete, I tried to write enough so the column would at least appear in the lister.

My process was to create an array of the items within the folder, then use a function to find the position of the item in the array.

Based on Post #2, I have to create my own “IndexOf” function to retrieve that position.

The issue, is based on my code, the function returns the count of all of the items in the folder for the value of each individual item, instead of the position of the item in the array.

My Questions

  1. How do I get my script to work based on tweaking the function to return the position of the item in the array?
  2. Is there a better method to writing this script? Especially faster?
  3. Is there a way which on intuitively understand the sort function for Jscript? Primarily regarding the direction of sort.
The Script
 function OnInit(initData)
{
    // Provide basic information about the script by initializing the properties of the ScriptInitData object
    initData.name = "Deco Relative Order";
    initData.desc = "Created Date Order";
    initData.copyright = "(c)";
    initData.default_enable = true; // Create a new ScriptColumn object and initialize it to add the column to Opus
    var cmd = initData.AddColumn();
    cmd.name = "DRelOrder";
    cmd.method = "OnRelOrder";
    cmd.label = "DRL";
    cmd.autogroup = true; // we provide our own grouping information
    cmd.autorefresh = true; // refresh column when files change
    cmd.justify = "left";
    cmd.match.push_back("Yes"); // when searching, these are the only two options
    cmd.match.push_back("No");
}

 function OnRelOrder(scriptcolumndata){

    
  var filesenum = new Enumerator(scriptcolumndata.tab.files);

 var filesarray = new Array();
 i = 0 ;
var z = 0;
 relitem = scriptcolumndata.item;

 while(!filesenum.atEnd())
    {
        filesarray[i++] = filesenum.item();
        filesenum.moveNext();
    }

filesarray.sort(function (a,b)
    {
        if (a.create > b.create)
        {
            return 1
        };
        if (a.create < b.create)
        {
            return -1
        };
        return 0
    })
    
function arraycontains(array, value){
    for(j = 0, k = filesarray.length; j < k; ++j){
        if(array[j] === value){
            return true;
        }
    }
return j;
    }
    //return i;
scriptcolumndata.value = arraycontains(filesarray, relitem) /*function arraycontains(filesarray, relitem){
        for (var j = 0, k = filesarray.length; j < k; ++j){
         if(filesarray[j] == relitem){
        return true;
    }
    return false;
        }*/
//scriptcolumndata.type = Number     
 }
 //}
 
 //arraycontains(filesarray, relitem)
 

Thank you,

Keep in mind that when the file display content changes (eg. by expanding/collapsing a folder or removing/adding items), it won't automatically trigger the column to be recalculated, so it may start showing incorrect values. You'd need to refresh it manually after each change.

If you ignore that detail, you can do it in something as simple as this. (Not tested)

function OnInit(initData) {
	initData.name = 'sort-create-test';
	initData.version = '1.0';
	initData.copyright = '(c) 2026 Cris';
	//	initData.url = 'https://resource.dopus.com/c/buttons-scripts/16';
	initData.desc = '';
	initData.default_enable = true;
	initData.min_version = '13.0';

}

// Called to add columns to Opus
function OnAddColumns(addColData) {
	var col = addColData.AddColumn();
	col.name = 'sort_create_test';
	col.method = 'Onsort_create_test';
	col.justify = 'right';
	col.type = 'number,signed';
	col.autogroup = true;
}

var obj;
// Implement the sort_create_test column
function Onsort_create_test(scriptColData) {
	var item = scriptColData.item;
	if (!item) return;
	var tab = scriptColData.tab;
	if (!tab) {
		scriptColData.value = 1;
		return;
	}
	if (!obj) obj = BuildArray(tab);
	scriptColData.value = obj[item.def_value] || -1;
}

function BuildArray(tab) {
	var arr = [];
	var items = tab.all;
	for (var i = items.count - 1; i >= 0; i--) {
		try {
			var create = items(i).create.GetTime();
			arr.push({
				key: items(i),
				value: create
			});
		}
		catch (err) {
			DOpus.Output('Something has failed bla bla...');
		};
	}
	arr.sort(function(A, B) {
		return B.value - A.value;
	});
	var obj = {};
	for (var i = 0; i < arr.length; i++) {
		var item = arr[i].key;
		obj[item.def_value] = i + 1;
	}
	return obj;
}

sort-create-test.opusscriptinstall (1.1 KB)

Thanks, I just looking through the code trying to understand the process.

So in JScript one can put an associative array in in element of an array?

Yes... AFAIK there shouldn't be any limitation on what you can put in an array.

Hello, sometimes when I refresh a lister tab, I get this absurdly large number (18,446,744,073,709,551,615) in the index column.

I think it happens after I do a find on the folder of the tab.