1

I am using JsTree plugin to draw a tree of folders and files, here you can see my sort mathode.

'sort' : function(a, b) {
        a1 = this.get_node(a);
        b1 = this.get_node(b);
        if (a1.icon == b1.icon){
            return (a1.text > b1.text) ? 1 : -1;
        } else {
            return (a1.icon > b1.icon) ? 1 : -1;
        }

This piece of code give me result like

  1. AA
  2. BB
  3. aa

its mean folders having name start with small letter,always going to be show after capital letter folder name.According to ASCII Capital letters Always sort first and small letter sort after capital letters. But i want to sort folder name such as

  1. AA

  2. aa

  3. BB

I mean folder must be sort alphabetically order by ignoring case letter.Thanks in Advance

sameer Ahmed
  • 537
  • 1
  • 5
  • 15
  • Possible duplicate of [JavaScript case insensitive string comparison](https://stackoverflow.com/questions/2140627/javascript-case-insensitive-string-comparison) – fennel Mar 26 '18 at 18:52

1 Answers1

3

An alternative is either toUpperCase or toLowerCase the strings:

Recommendation: use the function localeCompare to make the comparison between both strings.

The localeCompare() method returns a number indicating whether a reference string comes before or after or is the same as the given string in sort order.

if (a1.icon == b1.icon){
    return a1.text.toLowerCase().localeCompare(b1.text.toLowerCase());
} else {
    return a1.icon.toLowerCase().localeCompare(b1.icon.toLowerCase());
}
Ele
  • 33,468
  • 7
  • 37
  • 75