What does this code mean?
if( item.compareTo(root.element) < 0 ){
}
I read that:
"Compares two strings lexicographically. Returns an integer indicating whether this string is greater than (result is > 0), equal to (result is = 0), or less than (result is < 0) the argument."
But I don't don't get it. Can someone explain with an example please?
Take a look at the documentation of the Comparable
interface, which defines the compareTo()
method in the first place. The implementation of this interface in String
follows the same conventions:
Compares this object with the specified object for order. Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object
This means: if the current string is less than the string received as parameter (under the lexicographical order) return a negative integer value. If the current string is greater than the string received as parameter, return a positive integer value. Otherwise, the strings are equal and 0
is returned.
You would use this in sorting code to see if item
belongs before root.element
or not.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With