It doesn’t do a simple sum of the characters values, because then “ab” would be treated the same as “ba”, which would be useless for sorting. You can find a bit more of how this works here:
It’s useful for sorting, e.g. in Array.sort_custom() function. It’s even mentioned there in the documentation as an example of proper sorting of array of Strings.
Oh thanks for the link it’s much more clear now! I also see why it’d be useful.
I only found stuff with Java examples for some reason when I researched
I tested this and it seems in short what it does is it walks through each character of the strings a and b from left to right and as soon as 1 character at the same position in both strings is different from one another then the characters on the right are ignored (the check stops) and the character from both strings are evaluated. Correct?
func _ready() -> void:
var a: String = "Teresaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
var b: String = "Teresaba"
# a is shorter than the "to" argument which is b
# so it return -1 since "a" = 65 VS b = "66"
print(a.naturalnocasecmp_to(b))
More or less yes. Except when it’s comparing numbers - and that’s where it’s useful.
E.g. string “a2” would be sorted before “a10”, even though looking at the second character (1 vs 2) suggests that “a10” should be before “a2”, because 1<2.
Do your test again, but mix letters and numbers together and you will have a full understanding.
Thanks I tried mixing numbers and letters and I now see how it works with numbers in addition to what I understood above.
Hopefully it can help someone else as well!