Jump to content

User:Incnis Mrsi/Dictsort categories.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
/*
 Sorting categories alphabetically.
 The script is partially based on [[User:Anomie/previewtemplatelastmod.js]]
*/
 
function DictsortCategories() {
        var node=document.getElementById('mw-normal-catlinks');
//        alert('mw-normal-catlinks.innerHTML = ' + node.innerHTML);
        if(!node) return;
        var li = Array.prototype.slice.call(node.getElementsByTagName('li'));
//        alert('li.length = ' + li.length);
        if(li.length<=1) return;
 
        li.sort( function(a,b){ return (a.firstChild.title<b.firstChild.title)?1:(a.firstChild.title>b.firstChild.title)?-1:0; });
        var cur=li[0].parentNode.firstChild;
        for(var j=li.length-1; j>=0; j--){
            if(li[j]===cur){
                cur=cur.nextSibling;
            } else {
                cur.parentNode.insertBefore(li[j],cur);
            }
        }
        node.firstChild.href = "/wiki/User:Incnis_Mrsi/Dictsort_categories";
        node.firstChild.title = "(sorted alphabetically)";
        node.firstChild.innerHTML += '\u25C4';
//        alert(li.length + ' categories sorted');
};
 
addOnloadHook(DictsortCategories);

/*
	[[Category:Wikipedia scripts]]
*/