I’m trying to compare 2 strings alphabetically for sorting purposes. For example I want to have a boolean check like if('aaaa' < 'ab'). I tried it, but it’s not giving me correct results, so I guess that’s not the right syntax. How do I do this in jquery or Javascript?
I’m trying to compare 2 strings alphabetically for sorting purposes. For example I want
Share
Lets look at some test cases – try running the following expressions in your JS console:
All return true.
JavaScript compares strings character by character and “a” comes before “b” in the alphabet – hence less than.
In your case it works like so –
compares the first two “a” characters – all equal, lets move to the next character.
compares the second characters “a” against “b” – whoop! “a” comes before “b”. Returns true.