How to make skills/items/equips sort alphabetically (optionally controlled by switch)?

Hello,

As per the title, I am looking for any sort of code that allows me to sort skills, items and equip menu alphabetically. Optionally, I'd want it to be conditional (like checking for a switch), but I haven't found a way to put custom switch flipping into Visustella's Options Core. I had gotten the sorting to work for the skill and item window (see below), but opening the equip menu for any character that has something equipped would crash the game (and putting switch conditions wouldn't let me switch between on/off for some reason). Any help with this would be greatly appreciated. If it helps, I am using the majority of Visustella's plugins.


Code that worked for skills/items but crashed on equip with the message 'cannot read property 'name' of null':
JavaScript:
 if(a.name.toLowerCase() < b.name.toLowerCase()) return -1;
 if(a.name.toLowerCase() > b.name.toLowerCase()) return 1;
 return 0;
Optionally, I'd want it to be conditional (like checking for a switch), but I haven't found a way to put custom switch flipping into Visustella's Options Core.
Game switches, as implemented for the Control Switches event command, are included in save data and initialise on new game. By contrast, the in-game options apply globally, i.e. for all playthroughs. Generally you'd just check the config setting directly; I've provided an example below.

Code that worked for skills/items but crashed on equip with the message 'cannot read property 'name' of null':
JavaScript:
 if(a.name.toLowerCase() < b.name.toLowerCase()) return -1;
 if(a.name.toLowerCase() > b.name.toLowerCase()) return 1;
 return 0;
This looks like a callback for Array#sort?
If so, a return value of 0 should leave the order unchanged. So you can just check your config setting and return 0 if sorting is off/disabled, e.g.
JavaScript:
if (!ConfigManager.sortItems) return 0;
(This assumes the symbol for your config setting is sortItems.)

Alternatively, depending on how you're actually implementing the sorting, just stick the whole arr.sort(etc) in that if branch, so it only sorts when it needs to.

You may also want to confirm that you're sorting the correct "equips" list:
  • Empty equip slots (Window_EquipSlot) contain null.
  • I think Window_ItemList and its children (e.g. Window_EquipItem) do not usually include a null entry.
If necessary you can add a check for null values, e.g.
JavaScript:
if (a === null || b === null) return 0;
I.e. "do not change order if either entry is null".
After a bit of testing, the null check value and the config manager thing worked perfectly! Thank you! However, I'm not sure I'm familiar enough with JS to understand why it did/didn't work 😅
😂 Great! If it's working now then that's the main thing~

Brief explanation:
  • You can tell an array (a.k.a. "list") to sort itself according to some rules (a "callback" function) that you provide. That's what the code you cited is doing: for two entries from the array, it returns a value indicating which one should go first.

  • The error was because you were telling the game to check the name property of each entry, but the list had one or more entries that were null. null cannot have any properties, hence "cannot read property name of null".

Original thread: https://forums.rpgmakerweb.com/threads/how-to-make-skills-items-equips-sort-alphabetically-optionally-controlled-by-switch.157479/