Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare Strings as if they were numbers

I was wondering if it is possible to compare strings as if they were numbers. For instance is there any way you could make it so that "Cat" > "Dog"

like image 350
Steffan Harris Avatar asked Jan 20 '23 23:01

Steffan Harris


1 Answers

You can't use operators (e.g. "Cat" < "Dog") as you suggest. As @larsmans says that would require operator overloading which Java doesn't provide. However, you can still compare strings using "Cat".compareTo("Dog") which returns 0 if the strings are equal, a number greater than 0 if "Cat" is lexicographically less than "Dog", or a negative number otherwise.

See this page

like image 97
moinudin Avatar answered Jan 30 '23 19:01

moinudin