C
Second https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comparison_Operators :Strings are compared based on standard lexographical ordering, using Unicode values.That is, lexicographical comparison is made, taking into account the Unicode code points of the string. To better understand what a code point is, https://pt.stackoverflow.com/q/394834/112052 .But in a very short way, each character (and this is not restricted to letters, but also to digits, spaces, punctuation signals, emojis, etc.) has an associated numerical value, called code point. When two strings are compared, the numerical values (code points) corresponding to each character are taken into account in comparison.In the case, the letter a corresponding to http://www.fileformat.info/info/unicode/char/0061/index.htm (61 in hexadecimal, or 97 in decimal), and the letter b, http://www.fileformat.info/info/unicode/char/0062/index.htm (62 in hexa, 98 in decimal). So the string 'a' is considered "minor" that the string 'b'.And that has nothing to do with the string size:console.log('abacate' < 'bola'); // true
console.log('abacate' < 'abra'); // trueSecond https://tc39.es/ecma262/#sec-abstract-relational-comparison (item 3, if both operands are strings), what happens is that the first character of each string (the value of your code points) is compared. If they are equal, compare the second, and so on, until you find any that is different.In the first case ('abacate' < 'bola'), the first caracateres of each string are a and b and how a is smaller than b (the code point a is smaller than the code point b), then the string 'abacate' is "minor" that the string 'bola'.In the second case ('abacate' < 'abra'), the first and second character of the strings are equal (both begin with 'ab'), but when arriving in the third character, we have a and r, and how a it's "minor" r (for the code point of a is U+0061 and r It is http://www.fileformat.info/info/unicode/char/72/index.htm ), then the string 'abacate' is smaller than the string 'abra'.The string size is only relevant in cases like this:console.log('aba' < 'abacate'); // trueRemembering that this is not restricted to the letters, because each existing character has a code point. Then we can have things like:console.log('' > '丵124'); // trueFor the emoji "" also has a code point ( https://www.fileformat.info/info/unicode/char/1f4a9/index.htm ), whose value is greater than the character code point 丵 ( https://www.fileformat.info/info/unicode/char/4e35/index.htm ).And it is worth remembering a classic "armadilla", which is to compare strings that contain digits:console.log('10000' > '2'); // falseAs we are comparing strings, account is taken of the code point of the characters, and the character 1 possesses http://www.fileformat.info/info/unicode/char/31/index.htm , while the character 2 possesses http://www.fileformat.info/info/unicode/char/32/index.htm , and therefore string '10000' is considered smaller than the string '2'.If you want to compare numeric values, you should turn strings into numbers, for example using https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt :console.log(parseInt('10000') > parseInt('2')); // true, pois agora são números, e não stringsIt is worth remembering that Unicode also hides its own "armadillas":console.log('á' < 'á'); // trueThis happens because the first á is in NFD, and the second in NFC. To better understand what that is, I suggest you read https://pt.stackoverflow.com/q/406545/112052 , https://pt.stackoverflow.com/a/396555/112052 and https://pt.stackoverflow.com/a/345946/112052 . But to summarize, Unicode defines two different ways of representing the letter a with acute accent:composite form (NFC), as a single code point: the character itself áComposite form (NFD), as two code points: the character a (no accent) and http://www.fileformat.info/info/unicode/char/0301/index.htm Only that both, when shown on screen, appear in the same way (á), and only "hipping bits" of strings to see how many code points there are:// mostrar codepoints da string
function codepoints(s) { return Array.from(s).map(c => c.codePointAt(0).toString(16)).join(' '); }
// string em NFD, possui 2 code points
console.log(codepoints('á')); // 61 301
// string em NFC, possui 1 code point
console.log(codepoints('á')); // e1Thus, the first string above actually has two code points, the first being the letter a, which we have seen is the code point U+0061, but the first code point of the second string corresponds to the character á, whose value is http://www.fileformat.info/info/unicode/char/e1/index.htm , and therefore the first string is considered "minor".This can be solved by normalizing both to the same form (something as 'á'.normalize('NFC'), for example), but what to do exactly will depend on each case.There is also the method https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/localeCompare to compare strings according to one locale specific (i.e. according to the rules of a given language, because this varies a lot: accented characters can come before or after non-accepted, there are languages in which the alphabetical order is different, etc). But I believe you already run a little from the scope of the question (anyhow, you can see more details https://pt.stackoverflow.com/q/445795/112052 ).