{"record":{"id":"eabc6dd43d89b9fa","repo":"stanfordnlp/CoreNLP","slug":"neither-element-of-pair-comparable","errorCode":null,"errorMessage":"Neither element of pair comparable","messagePattern":"Neither element of pair comparable","errorType":"exception","errorClass":"AssertionError","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/util/Pair.java","lineNumber":156,"sourceCode":"   * @throws ClassCastException if the argument is not a\n   *                            {@code Pair}.\n   * @see java.lang.Comparable\n   */\n  @SuppressWarnings(\"unchecked\")\n  public int compareTo(Pair<T1,T2> another) {\n    if (first() instanceof Comparable) {\n      int comp = ((Comparable<T1>) first()).compareTo(another.first());\n      if (comp != 0) {\n        return comp;\n      }\n    }\n\n    if (second() instanceof Comparable) {\n      return ((Comparable<T2>) second()).compareTo(another.second());\n    }\n\n    if ((!(first() instanceof Comparable)) && (!(second() instanceof Comparable))) {\n      throw new AssertionError(\"Neither element of pair comparable\");\n    }\n\n    return 0;\n  }\n\n  /**\n   * If first and second are Strings, then this returns an MutableInternedPair\n   * where the Strings have been interned, and if this Pair is serialized\n   * and then deserialized, first and second are interned upon\n   * deserialization.\n   *\n   * @param p A pair of Strings\n   * @return MutableInternedPair, with same first and second as this.\n   */\n  public static Pair<String, String> stringIntern(Pair<String, String> p) {\n    return new MutableInternedPair(p);\n  }\n","sourceCodeStart":138,"sourceCodeEnd":174,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/util/Pair.java#L138-L174","documentation":"Pair.compareTo compares first elements, falling back to second elements; if neither element of either pair is Comparable and the pairs are not equal, it throws AssertionError 'Neither element of pair comparable'. It signals that the pair cannot define a total order with the elements it holds.","triggerScenarios":"Sorting or inserting into a sorted collection a Pair whose first (and second) elements do not implement Comparable, so compareTo cannot order two unequal pairs.","commonSituations":"Calling Collections.sort on List<Pair<CustomType, X>> where CustomType has no compareTo; using Pairs as keys in TreeMap/TreeSet with non-comparable element types; generics where T1/T2 were not constrained to Comparable.","solutions":["Make the pair's element class implement Comparable (or use a Comparator) for ordering","Sort with an explicit Comparator<Pair<T1,T2>> instead of relying on Pair's natural ordering","Use a HashSet/HashMap rather than sorted collections if ordering is unnecessary","Constrain generics to <T1 extends Comparable<T1>, ...> when creating the pairs"],"exampleFix":"// before\nCollections.sort(pairs); // Pair<MyObj,String> where MyObj not Comparable\n// after\npairs.sort(Comparator.comparing(Pair::first, myObjComparator));","handlingStrategy":"type-guard","validationCode":"static <T1,T2> boolean pairComparable(Pair<T1,T2> p) {\n  return p.first() instanceof Comparable || p.second() instanceof Comparable;\n}","typeGuard":"if (!(p.first() instanceof Comparable) && !(p.second() instanceof Comparable)) {\n  throw new IllegalArgumentException(\"Pair elements not comparable: \" + p);\n}","tryCatchPattern":"try {\n  Collections.sort(pairs);\n} catch (AssertionError e) {\n  if (e.getMessage().contains(\"comparable\")) {\n    pairs.sort(Comparator.comparing(Pair::toString)); // explicit fallback order\n  } else throw e;\n}","preventionTips":["Make pair element types implement Comparable when pairs will be sorted","Always sort Pairs with an explicit Comparator instead of natural ordering","Constrain generics to Comparable bounds at creation time","Avoid using Pairs as keys in TreeMap/TreeSet unless comparable"],"tags":["comparison","sorting","comparable","java"],"backgroundTag":"unsupported-operation","analyzedSha":"1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a","analyzedAt":"2026-09-10T02:24:07.274Z","contentChangedAt":"2026-09-10T02:24:07.274Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}