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 can only order collections whose element type implements java.lang.Comparable. The compiler inspects the collection's element type (defaulting to OBJECT when unknown) and rejects non-Comparable element types such as Thrift structs or maps.

Source

Thrown at botmaker/src/java/com/twitter/botmaker/compiler/Compiler.java:1037

                "Sort is expected to return %s but passed function returns %s",
                Type.LONG,
                funcNode.getReturnType()));
      }

      context.removeScope();

      ASTNode result = Sort.toSort(
          exprText, varA, varB, funcNode, context.currentScopeId(), collectionNode);
      return result;
    } else if (root.getChildCount() == 2) {
      ASTNode collectionNode = createASTNodeTree(context, root.getChild(1));
      Type elementType = Type.OBJECT;
      if (Collection.class.isAssignableFrom(collectionNode.getReturnType().typeBase)) {
        elementType = collectionNode.getReturnType().getTypeParams().get(0);
      }

      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)
        );
      }

      ASTNode result = Sort.toSort(exprText, collectionNode);
      return result;
    } else {
      throw new SemanticCheckFailure(String.format(
          "%s node with invalid number of children: %d", root.getText(), root.getChildCount()));
    }
  }

  private ASTNode toBlock(CompilerContext context, Tree root)
      throws ParseFailure, SemanticCheckFailure {
    if (root.getChildCount() == 0) {
      throw new SemanticCheckFailure("block body cannot be empty");

View on GitHub (pinned to 24c60942c5)

Solutions

  1. Use the two-argument Sort(coll, comparatorFn) form returning a Long key instead of the bare Sort
  2. Convert elements to Comparable values (e.g. extract a Long/String field) before sorting
  3. Ensure the collection is typed so its element type is known and Comparable

Example fix

// before
result = Sort(structList)
// after
result = Sort(structList, (a, b) -> a.score - b.score)
Defensive patterns

Strategy: type-guard

Validate before calling

Type elem = collectionType.getTypeParams().get(0);
if (!Comparable.class.isAssignableFrom(elem.typeBase)) useComparatorSort();

Type guard

static boolean isSortable(Type elementType) { return Comparable.class.isAssignableFrom(elementType.typeBase); }

Try / catch

catch (SemanticCheckFailure e) { if (e.getMessage().contains("Sort expects a collection")) switchToComparatorForm(); else throw e; }

Prevention

When it happens

Trigger: Sorting a collection of ThriftStruct objects, maps, or an untyped collection (element inferred as OBJECT) that is not Comparable.

Common situations: Trying to Sort structs directly instead of sorting with a comparator key, or sorting a heterogeneous/untyped collection where the element type cannot be proven Comparable.

Related errors


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