I’m using the clojure sort function to sort lists which are not homogeneous. this seems to trigger an error in the java TimSort function. Should that be considered my fault, or perhaps a clojure bug? Once I identified the issue, I easily worked around it by sorting a different way.
Execution error (ClassCastException) at java.util.TimSort/countRunAndMakeAscending (TimSort.java:355).
class clojure.lang.Cons cannot be cast to class java.lang.Comparable (clojure.lang.Cons is in unnamed module of loader 'app'; java.lang.Comparable is in module java.base of loader 'bootstrap')by “not homogeneous” I mean lists of very different lengths and depths, but consisting of clojure objects such as integers, lists, vectors, symbols, keywords etc.
> Should that be considered my fault, or perhaps a clojure bug? The former. According to the error, you're asking Clojure to sort something that contains items that cannot be compared.
Did you decide to use sort-by? That can be useful for this kind of case
to play the devils advocate a bit, shouldn’t all the build-in clojure objects already implement “Comparable”?
@phill, actually I was sorting a list of pairs (each pair is a vector of list and integer). I really wanted to sort by this integer, so yes I changed it to call sort-by second …. on the other hand I was surprised that lists of primitive object would trigger this error.
> to play the devils advocate a bit, shouldn’t all the build-in clojure objects already implement “Comparable”? Perhaps, there's an issue on making lists Comparable: https://clojure.atlassian.net/browse/CLJ-1467