Filename.compareTo

How does one interpret the output of “Filename.compareTo”? A bit of experimentation seems to suggest that a value of “0” indicates that the Filenames in question are equal, but I’d like to check that, and it’s not clear to me what other values indicate.

Furthermore, what does the “compareTo” method consider to constitute “equality”?

It’s like the old Python 2 cmp built-in function: it returns 1 if it’s larger than the other, 0 if they are equal, -1 if it’s smaller than the other.

Basically, the output of fn1 > fn2 is the same as fn1.compareTo(fn2) > 0, when replacing > with any comparison operator of choice.

However, there’s no good reason to not simply use the == and < operators directly if you need to compare filenames.

The comparison operators just do a string comparison of getFullpath() (aka str(filename)). This also applies to compareTo.

I’m not actually really sure why compareTo exists, I think for a while interrogate needed it to be able to generate a Python 2 comparison function, but this hasn’t been the case for a long time since Python 2 got rich comparison operators.

I would suggest that you avoid compareTo unless you really need to know whether it is equal and what kind of inequality it is in one operation.

Ah, fair enough, and thank you for the explanation! As you say, it sounds like the thing to do is to simply use the equality operator for my purposes. :slight_smile:

(Although I’ll note that I get the impression that “compareTo” goes a bit further than 1 or -1, at least–I’m pretty sure that I’ve seen an output of 7.)

It’s possible that the semantics have actually been defined more loosely whan what I described above, so a correction may be in order:

It’s like the old Python 2 cmp built-in function: it returns a positive number if it’s larger than the other, 0 if they are equal, a negative number if it’s smaller than the other.

Ah, fair enough! Thank you for the clarification. :slight_smile: