I was working on some code where I need to perform an alphabetic ascending sort (standard .sort()) behavior and I wondered how sorting works with special characters and characters from different character sets. For example:
var aChars = [];
aChars.push("¢");
aChars.push("™");
aChars.push("È");
aChars.push("~");
aChars.push("p");
aChars.push("_");
aChars.push("P");
aChars.push("治");
aChars.push("す");
aChars.sort();
for (var i = 0;i<aChars.length;i++) {
console.log(aChars[i]);
//alert(aChars[i]);
}
I always get a consistent order when I execute this code. How does JavaScript decide the order? I thought it might be by HTML entity number, but I can’t find an HTML entity number for those Japanese characters.
If you don’t provide your own comparision function, then per the ECMAScript spec, here are the rules:
For your particular case, it is determined by steps 7-11.
And, the
<and>operators compare the Unicode encoding value of the first differing character.For finer grain control over the situation, MDN recommends using your own custom compare function that uses
String.localeCompare()which contains a bit smarter logic for sorting characters.