I am getting the Comparison method violates its general contract exception with this compareTo method, but I can’t track down what is exactly causing the issue. I am trying to sort files by their extension in a particular way. Mind you this doesn’t happen for all phones, just ones that I don’t have access to which makes it harder to test out.
public int compareTo(NzbFile another)
{
if (this.getFileName() != null && another.getFileName() != null)
{
if (this.getFileName().toLowerCase().endsWith(".nfo"))
return -1000;
else if (another.getFileName().toLowerCase().endsWith(".nfo"))
return 1000;
else if (this.getFileName().toLowerCase().endsWith(".sfv"))
return -999;
else if (another.getFileName().toLowerCase().endsWith(".sfv"))
return 1001;
else if (this.getFileName().toLowerCase().endsWith(".srr"))
return -998;
else if (another.getFileName().toLowerCase().endsWith(".srr"))
return 1002;
else if (this.getFileName().toLowerCase().endsWith(".nzb"))
return -997;
else if (another.getFileName().toLowerCase().endsWith(".nzb"))
return 1003;
else if (this.getFileName().toLowerCase().endsWith(".srt"))
return -996;
else if (another.getFileName().toLowerCase().endsWith(".srt"))
return 1004;
else
return this.getFileName().compareTo(another.getFileName());
}
else if (this.getFileName() != null && another.getFileName() == null)
{
return -995;
}
else if (this.getFileName() == null && another.getFileName() != null)
{
return 1005;
}
else
{
return this.getSubject().compareTo(another.getSubject());
}
}
If your file names are the same, and both e.g. end in
.nfo, then this will return that they aren’t equal. I assume that they’re meant to be equal.I strongly suspect that there’s a Better Way to Do This. I’ll use Guava for my example, but it’s not strictly necessary.