string - Why does javascript localeCompare sort underscores before periods? -


i'm trying compare strings sorting purposes, , want sort periods before underscores (because follows ascii table). suggestions / explanations why localecompare doesn't way?

as example, want tester.java come before tester_1.java

"apple".localecompare("tomatoe") returns -1 because apple smaller "tester.java".localecompare("tester_1.java") returns 1 when want return -1

i think mean .localecompare() right (this mispelled in question)? way ordering being done? possibly have them wrong way around:

var array = [ "_a", ".b" ] array.sort()             // returns [ ".b", "_a" ] array.sort(function(a,b) { return a.localecompare(b) }) // returns [ ".b", "_a" ] array.sort(function(a,b) { return b.localecompare(a) }) // returns [ "_a", ".b" ] 

looking @ under hood:

var str1 = "_a",     str2 = ".b";  str1.localecompare(str2)           // 49 str2.localecompare(str1)           // -49 

so if comparing in wrong order "reversed"

var array = [ ".c", "_a", ".b" ] array.sort(function(a,b) { return a.localecompare(b) })  // [ ".b", ".c", "_a" ] array.sort(function(a,b) { return b.localecompare(a) })  // [ "_a", ".c", ".b" ] 

Comments

Popular posts from this blog

php - Zend Framework / Skeleton-Application / Composer install issue -

c# - Better 64-bit byte array hash -

python - PyCharm Type error Message -