You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何忽略大小写对JsTree(jQuery插件)节点进行字母排序

Fixing Case-Insensitive Sorting in jsTree

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.text and b1.text to lowercase (you could also use toUpperCase()—either works), we eliminate case differences before comparing. So AA and aa both become aa, and Banana vs apple becomes banana vs apple, which sorts correctly as apple, 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 07:47:53