0

the source of the line is provided below, i understand what v.compareTo(w) does but i am curious as to what influence the < 0 has, does it not allow it to return if compareTo returns 1 which is greater than 0? does it not return if anything if compareTo returns 0 because it is equal to 0?

private static boolean less(Comparable v, Comparable w)   
{  
    return v.compareTo(w) < 0; 
}  
3
Contributors
2
Replies
13
Views
1 Year
Discussion Span
Last Post by rubberman
0

compareTo returns an int...
v<w returns -1
v==w returns 0
v>w returns +1

so if v is less than w, compareTo will return -1, and line 3 will return true. Otherwize compareTo will return 0 or+1, and line 3 will return false.

Edited by JamesCherrill

0

This is the usual way to sort lists of stuff, and a number of the C++, C#, and Java (as well as other OO languages) use this method to determine where to insert an element in a map or other sorted list.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.