{"record":{"id":"7873343c358fbf13","repo":"xai-org/x-algorithm","slug":"sort-expects-a-collection-of-s-objects-but-gets-787334","errorCode":null,"errorMessage":"Sort expects a collection of %s objects, but gets %s","messagePattern":"Sort expects a collection of (.+?) objects, but gets (.+?)","errorType":"validation","errorClass":"SemanticCheckFailure","httpStatus":null,"severity":"error","filePath":"botmaker/src/java/com/twitter/botmaker/function/collection/Sort.java","lineNumber":53,"sourceCode":"      Type.listOf(TP2)\n  );\n\n  private Sort() {\n  }\n\n  public static ASTNode toSort(\n      String exprText,\n      ASTNode collectionNode) throws SemanticCheckFailure {\n\n    final Type elementType;\n    if (Collection.class.isAssignableFrom(collectionNode.getReturnType().typeBase)) {\n      elementType = collectionNode.getReturnType().getTypeParams().get(0);\n    } else {\n      elementType = Type.OBJECT;\n    }\n\n    if (!Comparable.class.isAssignableFrom(elementType.typeBase)) {\n      throw new SemanticCheckFailure(\n          String.format(\"Sort expects a collection of %s objects, but gets %s\",\n              Comparable.class.getName(),\n              elementType)\n      );\n    }\n\n    return new FunctionNode1<Runtime, Collection<Object>>(exprText,\n        ImmutableList.of(collectionNode)) {\n\n      @Override\n      public List<Comparable> apply(Context<Runtime> context, Collection<Object> collection) {\n        Collection<Comparable> comparableCollection = (Collection) collection;\n        List<Comparable> listToBeSorted = new ArrayList<>(comparableCollection);\n        Collections.sort(listToBeSorted);\n        return listToBeSorted;\n      }\n\n      @Override","sourceCodeStart":35,"sourceCodeEnd":71,"githubUrl":"https://github.com/xai-org/x-algorithm/blob/24c60942c5c5fdad3a6addffb4c6e6d2f228f04f/botmaker/src/java/com/twitter/botmaker/function/collection/Sort.java#L35-L71","documentation":"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.","triggerScenarios":"Sort(listOfPairs), Sort(listOfStructs), or Sort of a collection whose type param is Object or a non-Comparable class.","commonSituations":"Trying to sort heterogeneous or tuple/struct collections, or untyped collections where the element type param was erased to Object.","solutions":["Sort a collection whose element type is Comparable (String, Long, etc.)","For structs/tuples, use SortBy with an explicit comparable key extractor instead of Sort","Re-type the collection so its element type param is a Comparable class"],"exampleFix":"// before\nSort(listOfPairs)\n// after\nSortBy(listOfPairs, p -> Get(p, 0)) // sort by a comparable key","handlingStrategy":"type-guard","validationCode":"// rule-language: sort by an extracted comparable key\nSortBy(items, x -> Get(x, \"ts\"))","typeGuard":"// host code\nType elem = collectionNode.getReturnType().getTypeParams().get(0);\nboolean ok = Comparable.class.isAssignableFrom(elem.typeBase);","tryCatchPattern":null,"preventionTips":["Sort only collections of Comparable elements (String, Long, ...)","Use SortBy with a key extractor for structs/tuples","Ensure collections carry a concrete Comparable element type param"],"tags":["botmaker","semantic-check","type-mismatch","sort","comparable"],"backgroundTag":"function-argument-type-mismatch","analyzedSha":"24c60942c5c5fdad3a6addffb4c6e6d2f228f04f","analyzedAt":"2026-08-28T11:40:14.686Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}