xai-org/x-algorithm · error · SemanticCheckFailure

Sort expects a collection of %s objects, but gets %s

Error message

Sort expects a collection of %s objects, but gets %s

What it means

Sort() requires the collection's element type to implement Comparable. The element type is taken from the collection's first type parameter (defaulting to OBJECT when untyped, which is not Comparable), and non-Comparable elements fail semantic checking with this message showing the expected Comparable interface and the actual element type.

Source

Thrown at botmaker/src/java/com/twitter/botmaker/function/collection/Sort.java:53

      Type.listOf(TP2)
  );

  private Sort() {
  }

  public static ASTNode toSort(
      String exprText,
      ASTNode collectionNode) throws SemanticCheckFailure {

    final Type elementType;
    if (Collection.class.isAssignableFrom(collectionNode.getReturnType().typeBase)) {
      elementType = collectionNode.getReturnType().getTypeParams().get(0);
    } else {
      elementType = Type.OBJECT;
    }

    if (!Comparable.class.isAssignableFrom(elementType.typeBase)) {
      throw new SemanticCheckFailure(
          String.format("Sort expects a collection of %s objects, but gets %s",
              Comparable.class.getName(),
              elementType)
      );
    }

    return new FunctionNode1<Runtime, Collection<Object>>(exprText,
        ImmutableList.of(collectionNode)) {

      @Override
      public List<Comparable> apply(Context<Runtime> context, Collection<Object> collection) {
        Collection<Comparable> comparableCollection = (Collection) collection;
        List<Comparable> listToBeSorted = new ArrayList<>(comparableCollection);
        Collections.sort(listToBeSorted);
        return listToBeSorted;
      }

      @Override

View on GitHub (pinned to 24c60942c5)

Solutions

  1. Sort a collection whose element type is Comparable (String, Long, etc.)
  2. For structs/tuples, use SortBy with an explicit comparable key extractor instead of Sort
  3. Re-type the collection so its element type param is a Comparable class

Example fix

// before
Sort(listOfPairs)
// after
SortBy(listOfPairs, p -> Get(p, 0)) // sort by a comparable key
Defensive patterns

Strategy: type-guard

Validate before calling

// rule-language: sort by an extracted comparable key
SortBy(items, x -> Get(x, "ts"))

Type guard

// host code
Type elem = collectionNode.getReturnType().getTypeParams().get(0);
boolean ok = Comparable.class.isAssignableFrom(elem.typeBase);

Prevention

When it happens

Trigger: Sort(listOfPairs), Sort(listOfStructs), or Sort of a collection whose type param is Object or a non-Comparable class.

Common situations: Trying to sort heterogeneous or tuple/struct collections, or untyped collections where the element type param was erased to Object.

Related errors


AI-assisted analysis of xai-org/x-algorithm@24c60942c5 (2026-08-28). Data as JSON: /api/errors/7873343c358fbf13. Report an issue: GitHub.