Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page.
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.
/** * Sort references by date if they are in YMD/ISO 8601 format * To use, insert the following into your vector.js page: * importScript("User:Gyrobo/sortISODate.js"); **/varref_sorted_type=false;// Convert nodelist to array// http://stackoverflow.com/questions/2735067/how-to-convert-a-dom-node-list-to-an-array-in-javascriptfunctiontoArray(obj){vararray=[];// iterate backwards ensuring that length is an UInt32for(vari=obj.length>>>0;i--;){array[i]=obj[i];}returnarray;}functionsortReferencesByDate(){varreferences=[];vari=0;// FF 3.5+, Opera 10+, Safari 3.1+, Chrome, IE8+if(document.querySelectorAll){references=document.querySelectorAll("ol.references");}// You should really upgradeelse{returnfalse;}// Iterate through each reference blockfor(varl=0;l<references.length;l++){refs=references[l].getElementsByTagName("li");refs=toArray(refs);vardates=[];// Build date arrayfor(i=0;i<refs.length;i++){dates[dates.length]=refs[i].innerHTML.match(/\d{4}-\d{2}-\d{2}/);}// Now, sort them with a simple bubble sort (N^2 runtime)for(i=0;i<dates.length;i++){for(vark=0;k<dates.length-1;k++){if(dates[k+1]<dates[k]){vartd=dates[k];vartr=refs[k];dates[k]=dates[k+1];refs[k]=refs[k+1];dates[k+1]=td;refs[k+1]=tr;}}}// Should dates be reversed?if(ref_sorted_type){dates=dates.reverse();refs=refs.reverse();ref_sorted_type=false;}else{ref_sorted_type=true;}// Now add all the references back to their section in orderreferences[l].innerHTML="";for(i=0;i<refs.length;i++){references[l].appendChild(refs[i]);}}returnfalse;}functionaddSortingButton(){varreferences=[];// FF 3.5+, Opera 10+, Safari 3.1+, Chrome, IE8+if(document.querySelectorAll){references=document.querySelectorAll("ol.references");}// You should really upgradeelse{returnfalse;}for(vari=0;i<references.length;i++){varnode=references[i].parentNode;vara=document.createElement("a");a.href="#";a.onclick=sortReferencesByDate;a.innerHTML="[Sort by date]";node.insertBefore(a,references[i]);}}if(window.addEventListener){window.addEventListener("load",addSortingButton,false);}else{window.attachEvent("onload",addSortingButton);}