如何忽略大小写对JsTree(jQuery插件)节点进行字母排序
Got it, let's tackle this case-insensitive sorting issue for your jsTree folder/file tree. The problem with your current code is that it compares raw text strings directly—since uppercase letters have lower ASCII values than lowercase ones, you end up with all uppercase entries first, followed by lowercase ones (like AA, BB, aa).
Modified Sort Function
Here's the adjusted code that ignores case while keeping your existing icon-based grouping logic:
'sort': function(a, b) { const a1 = this.get_node(a); const b1 = this.get_node(b); // Maintain grouping by icon type first (folders vs files, etc.) if (a1.icon === b1.icon) { // Convert both text values to lowercase for case-insensitive comparison const lowerTextA = a1.text.toLowerCase(); const lowerTextB = b1.text.toLowerCase(); // Compare the normalized strings return lowerTextA > lowerTextB ? 1 : -1; } else { // Keep your original icon-based sorting logic here return a1.icon > b1.icon ? 1 : -1; } }
How This Works
- By converting both
a1.textandb1.textto lowercase (you could also usetoUpperCase()—either works), we eliminate case differences before comparing. SoAAandaaboth becomeaa, andBananavsapplebecomesbananavsapple, which sorts correctly asapple,Banana. - We retain your original icon comparison first, so folders (or any nodes with matching icons) stay grouped together before sorting by name.
Bonus: Better Multilingual/Special Character Support
If you need to handle non-English characters or special symbols more gracefully, replace the direct comparison with localeCompare():
return lowerTextA.localeCompare(lowerTextB);
localeCompare() handles language-specific sorting rules, making your tree more robust for international use.
内容的提问来源于stack exchange,提问作者sameer Ahmed

