<commit_before>
var sbSortService = {

	get WIZARD()      { return document.getElementById("sbSortWizard"); },
	get RADIO_GROUP() { return document.getElementById("sbSortRadioGroup"); },

	key : "",
	ascending : false,
	index : -1,
	contResList : [],
	waitTime : 0,

	init : function()
	{
		this.WIZARD.getButton("back").hidden = true;
		this.WIZARD.getButton("finish").disabled = true;
		this.WIZARD.getButton("next").removeAttribute("accesskey");
		this.WIZARD.canAdvance = false;
		this.RADIO_GROUP.selectedIndex = this.RADIO_GROUP.getAttribute("sortIndex");
		if ( window.arguments ) {
			this.contResList = [window.arguments[0]];
			this.waitTime = 2;
		} else {
			this.contResList = [sbCommonUtils.RDF.GetResource("urn:scrapbook:root")];
			this.waitTime = 6;
		}
		sbSortService.countDown();
		// hide the tree
		window.opener.document.getElementById("sbTreeOuter").hidden = true;
	},

	countDown : function()
	{
		this.WIZARD.getButton("next").label = sbCommonUtils.lang("scrapbook", "START_BUTTON") + (this.waitTime > 0 ? " (" + this.waitTime + ")" : "");
		this.WIZARD.canAdvance = this.waitTime == 0;
		if ( this.waitTime-- ) setTimeout(function(){ sbSortService.countDown() }, 500);
	},

	exec : function()
	{
		this.WIZARD.getButton("cancel").hidden = true;
		if (document.getElementById("sbSortRecursive").checked)
			this.contResList = sbDataSource.flattenResources(this.contResList[0], 1, true);
		switch ( this.RADIO_GROUP.selectedIndex )
		{
			case 0 : break;
			case 1 : this.key = "title"; this.ascending = true;  break;
			case 2 : this.key = "title"; this.ascending = false; break;
			case 3 : this.key = "id";    this.ascending = true;  break;
			case 4 : this.key = "id";    this.ascending = false; break;
		}
		this.next();
	},

	next : function()
	{
		if ( ++this.index < this.contResList.length ) {
			document.getElementById("sbSortTextbox").value = "(" + (this.index + 1) + "/" + this.contResList.length + ")... " + sbDataSource.getProperty(this.contResList[this.index], "title");
			this.process(this.contResList[this.index]);
		} else {
			// sort completed
			this.RADIO_GROUP.setAttribute("sortIndex", this.RADIO_GROUP.selectedIndex);
			window.close();
			// show the tree
			window.opener.document.getElementById("sbTreeOuter").hidden = false;
		}
	},

	process : function(aContRes)
	{
		var rdfCont = Components.classes['@mozilla.org/rdf/container;1'].createInstance(Components.interfaces.nsIRDFContainer);
		rdfCont.Init(sbDataSource.data, aContRes);
		var resEnum = rdfCont.GetElements();
		var resListF = [], resListI = [], resListN = [];
		if ( !this.key )
		{
			while ( resEnum.hasMoreElements() )
			{
				var res = resEnum.getNext().QueryInterface(Components.interfaces.nsIRDFResource);
				resListF.push(res);
			}
			resListF.reverse();
		}
		else
		{
			while ( resEnum.hasMoreElements() )
			{
				var res = resEnum.getNext().QueryInterface(Components.interfaces.nsIRDFResource);
				if ( sbDataSource.isContainer(res) )
					resListF.push(res);
				else
					( sbDataSource.getProperty(res, "type") == "note" ? resListN : resListI ).push(res);
			}
			resListF.sort(this.compare); if ( !this.ascending ) resListF.reverse();
			resListI.sort(this.compare); if ( !this.ascending ) resListI.reverse();
			resListN.sort(this.compare); if ( !this.ascending ) resListN.reverse();
			resListF = resListF.concat(resListI).concat(resListN);
		}
		for ( var i = 0; i < resListF.length; i++ )
		{
			rdfCont.RemoveElement(resListF[i], true);
			rdfCont.AppendElement(resListF[i]);
		}
		setTimeout(function(){ sbSortService.next(res); }, 0);
	},

	showTree : function()
	{
		// show the tree
		window.opener.document.getElementById("sbTreeOuter").hidden = false;
	},

	compare : function(resA, resB)
	{
		var a = sbDataSource.getProperty(resA, sbSortService.key).toUpperCase();
		var b = sbDataSource.getProperty(resB, sbSortService.key).toUpperCase();
		if ( a > b ) return 1;
		if ( a < b ) return -1;
		return 0;
	},

};


<commit_msg>Optimize the code by reducing reverse processes.<commit_after>
var sbSortService = {

	get WIZARD()      { return document.getElementById("sbSortWizard"); },
	get RADIO_GROUP() { return document.getElementById("sbSortRadioGroup"); },

	key : "",
	order : 1,
	index : -1,
	contResList : [],
	waitTime : 0,

	init : function()
	{
		this.WIZARD.getButton("back").hidden = true;
		this.WIZARD.getButton("finish").disabled = true;
		this.WIZARD.getButton("next").removeAttribute("accesskey");
		this.WIZARD.canAdvance = false;
		this.RADIO_GROUP.selectedIndex = this.RADIO_GROUP.getAttribute("sortIndex");
		if ( window.arguments ) {
			this.contResList = [window.arguments[0]];
			this.waitTime = 2;
		} else {
			this.contResList = [sbCommonUtils.RDF.GetResource("urn:scrapbook:root")];
			this.waitTime = 6;
		}
		sbSortService.countDown();
		// hide the tree
		window.opener.document.getElementById("sbTreeOuter").hidden = true;
	},

	countDown : function()
	{
		this.WIZARD.getButton("next").label = sbCommonUtils.lang("scrapbook", "START_BUTTON") + (this.waitTime > 0 ? " (" + this.waitTime + ")" : "");
		this.WIZARD.canAdvance = this.waitTime == 0;
		if ( this.waitTime-- ) setTimeout(function(){ sbSortService.countDown() }, 500);
	},

	exec : function()
	{
		this.WIZARD.getButton("cancel").hidden = true;
		if (document.getElementById("sbSortRecursive").checked)
			this.contResList = sbDataSource.flattenResources(this.contResList[0], 1, true);
		switch ( this.RADIO_GROUP.selectedIndex )
		{
			case 0 : break;
			case 1 : this.key = "title"; this.order = 1;  break;
			case 2 : this.key = "title"; this.order = -1; break;
			case 3 : this.key = "id";    this.order = 1;  break;
			case 4 : this.key = "id";    this.order = -1; break;
		}
		this.next();
	},

	next : function()
	{
		if ( ++this.index < this.contResList.length ) {
			document.getElementById("sbSortTextbox").value = "(" + (this.index + 1) + "/" + this.contResList.length + ")... " + sbDataSource.getProperty(this.contResList[this.index], "title");
			this.process(this.contResList[this.index]);
		} else {
			// sort completed
			this.RADIO_GROUP.setAttribute("sortIndex", this.RADIO_GROUP.selectedIndex);
			window.close();
			// show the tree
			window.opener.document.getElementById("sbTreeOuter").hidden = false;
		}
	},

	process : function(aContRes)
	{
		var rdfCont = Components.classes['@mozilla.org/rdf/container;1'].createInstance(Components.interfaces.nsIRDFContainer);
		rdfCont.Init(sbDataSource.data, aContRes);
		var resEnum = rdfCont.GetElements();
		var resListF = [], resListI = [], resListN = [];
		if ( !this.key )
		{
			while ( resEnum.hasMoreElements() )
			{
				var res = resEnum.getNext().QueryInterface(Components.interfaces.nsIRDFResource);
				resListF.push(res);
			}
			resListF.reverse();
		}
		else
		{
			while ( resEnum.hasMoreElements() )
			{
				var res = resEnum.getNext().QueryInterface(Components.interfaces.nsIRDFResource);
				switch(sbDataSource.getProperty(res, "type")) {
					case "folder":
						resListF.push(res);
						break;
					case "note":
						resListN.push(res);
						break;
					default:
						resListI.push(res);
						break;
				}
			}
			resListF.sort(this.compare);
			resListI.sort(this.compare);
			resListN.sort(this.compare);
			resListF = resListF.concat(resListI).concat(resListN);
		}
		for ( var i = 0; i < resListF.length; i++ )
		{
			rdfCont.RemoveElement(resListF[i], true);
			rdfCont.AppendElement(resListF[i]);
		}
		setTimeout(function(){ sbSortService.next(res); }, 0);
	},

	showTree : function()
	{
		// show the tree
		window.opener.document.getElementById("sbTreeOuter").hidden = false;
	},

	compare : function(resA, resB)
	{
		var a = sbDataSource.getProperty(resA, sbSortService.key).toUpperCase();
		var b = sbDataSource.getProperty(resB, sbSortService.key).toUpperCase();
		if ( a > b ) return sbSortService.order;
		if ( a < b ) return -sbSortService.order;
		return 0;
	},

};



{'fraction_non_alphanumeric': 0.16229177183240787, 'fraction_numerical': 0.006436143361938415, 'mean_word_length': 3.8470948012232418, 'pattern_counts': {'":': 2, '<': 9, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2624786', 'n_tokens_mistral': 3098, 'n_tokens_neox': 2749, 'n_words': 452}