Jump to content
 







Main menu
   


Navigation  



Main page
Contents
Current events
Random article
About Wikipedia
Contact us
Donate
 




Contribute  



Help
Learn to edit
Community portal
Recent changes
Upload file
 








Search  

































Create account

Log in
 









Create account
 Log in
 




Pages for logged out editors learn more  



Contributions
Talk
 

















User:Alex 21/script-categoriessort.js

















User page
Talk
 

















Read
View source
View history
 








Tools
   


Actions  



Read
View source
View history
 




General  



What links here
Related changes
User contributions
User logs
View user groups
Upload file
Special pages
Permanent link
Page information
Get shortened URL
Download QR code
 




Print/export  



Download as PDF
Printable version
 
















Appearance
   

 






From Wikipedia, the free encyclopedia
 

< User:Alex 21

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.
$(function($) {
 mw.loader.using( ['mediawiki.util'] ).then( function () {
  var portletlink = mw.util.addPortletLink('p-tb', '#', 'Sort categories');
  $(portletlink).click( function(e) {
   e.preventDefault();
   // Determine if we need to go to the editing page.
   var loc = window.location.href; 
   if (loc.indexOf('action=edit') < 0 && loc.indexOf('action=submit') < 0) {
    alert("Go to the edit page to use this script.");
   } else {
    // Get textbox value
    var wpTextbox1 = document.getElementById('wpTextbox1');
    var wpTextbox1_V = wpTextbox1.value;
    
    // Categories to check
    var category = "[[Category:";
    var categoryA = category+"A ";
    var categoryThe = category+"The ";
    var categoryEponymous = category+mw.config.get("wgTitle");
    
    // Get the text up to the start of the categories, and then all of the text with the categories.
    var categoriesStart = wpTextbox1_V.indexOf(category);
    var textBeforeCategories = wpTextbox1_V.substr(0, categoriesStart).trim();
    var textWithCategories = wpTextbox1_V.substr(categoriesStart).trim();
    
    // Categories should be on new lines, so split by new line, sort alphabetically with a few checks, then join again with new lines.
    // Checks: eponymous categories are listed first; categories are sorted without preceding "The"
    var splitCategories = textWithCategories.split("\n");
    splitCategories.sort(function(a, b) {
     if (a.substr(0, categoryEponymous.length) == categoryEponymous) return -1e8;
     if (b.substr(0, categoryEponymous.length) == categoryEponymous) return 1e8;
     
     if (a.substr(0, categoryThe.length) == categoryThe) a = a.replace(categoryThe, category);
     if (b.substr(0, categoryThe.length) == categoryThe) b = b.replace(categoryThe, category);
     
     if (a.substr(0, categoryA.length) == categoryA) a = a.replace(categoryA, category);
     if (b.substr(0, categoryA.length) == categoryA) b = b.replace(categoryA, category);
     
     return a.localeCompare(b);
    });
    textWithCategories = splitCategories.join("\n");
    
    // Merge pre-category text back with the sorted and joined category text, place back in textbok and add summary.
    wpTextbox1.value = textBeforeCategories+"\n"+textWithCategories+"\n";
    document.getElementById('wpSummary').value += "Sorted categories alphabetically via [[User:Alex 21/script-categoriessort|script]].";
    document.getElementById('wpMinoredit').checked = true;
   }
  });
 });
});

Retrieved from "https://en.wikipedia.org/w/index.php?title=User:Alex_21/script-categoriessort.js&oldid=1188900080"





This page was last edited on 8 December 2023, at 11:06 (UTC).

Text is available under the Creative Commons Attribution-ShareAlike License 4.0; additional terms may apply. By using this site, you agree to the Terms of Use and Privacy Policy. Wikipedia® is a registered trademark of the Wikimedia Foundation, Inc., a non-profit organization.



Privacy policy

About Wikipedia

Disclaimers

Contact Wikipedia

Code of Conduct

Developers

Statistics

Cookie statement

Mobile view



Wikimedia Foundation
Powered by MediaWiki