{"record":{"id":"85daf6a74455a22a","repo":"karatelabs/karate","slug":"sort-needs-at-least-one-argument-the-list-to-sort","errorCode":null,"errorMessage":"sort() needs at least one argument: the list to sort","messagePattern":"sort\\(\\) needs at least one argument: the list to sort","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"karate-core/src/main/java/io/karatelabs/core/KarateJsUtils.java","lineNumber":484,"sourceCode":"            if (args.length == 0) {\n                throw new RuntimeException(\"sizeOf() needs one argument\");\n            }\n            Object obj = args[0];\n            if (obj instanceof List) {\n                return ((List<?>) obj).size();\n            } else if (obj instanceof Map) {\n                return ((Map<?, ?>) obj).size();\n            } else if (obj instanceof String) {\n                return ((String) obj).length();\n            }\n            return 0;\n        };\n    }\n\n    static JavaInvokable sort() {\n        return args -> {\n            if (args.length == 0) {\n                throw new RuntimeException(\"sort() needs at least one argument: the list to sort\");\n            }\n            List<?> list = (List<?>) args[0];\n            // the key function is optional, without it items are compared as-is (natural ordering)\n            JavaCallable fn = args.length > 1 && args[1] instanceof JavaCallable ? (JavaCallable) args[1] : null;\n            int count = list.size();\n            // extract keys once up front instead of on every comparison\n            List<Object[]> pairs = new ArrayList<>(count);\n            for (int i = 0; i < count; i++) {\n                Object item = list.get(i);\n                Object key = fn == null ? item : fn.call(null, new Object[]{item, i});\n                pairs.add(new Object[]{key, item});\n            }\n            pairs.sort((a, b) -> compareKeys(a[0], b[0]));\n            List<Object> result = new ArrayList<>(count);\n            for (Object[] pair : pairs) {\n                result.add(pair[1]);\n            }\n            return result;","sourceCodeStart":466,"sourceCodeEnd":502,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-core/src/main/java/io/karatelabs/core/KarateJsUtils.java#L466-L502","documentation":"karate.sort() is a JS-callable helper in KarateJsUtils that sorts a list, optionally by a key function. The library throws this error when the function is invoked with zero arguments, because the list to sort is mandatory — there is no sensible default (no receiver list, no implicit variable). It is a fail-fast argument-count guard inside the JavaInvokable lambda.","triggerScenarios":"Calling karate.sort() (or the registered sort binding) with no arguments, e.g. calling sort without parentheses binding in JS, or a typo dropping the argument: karate.sort().","commonSituations":"Scripting data transformations in karate.set()/def expressions; refactoring a call like karate.sort(list, fn) and accidentally deleting the argument; dynamically building the call and the list variable being undefined so the call site collapses to no args.","solutions":["Pass the list as the first argument: karate.sort(myList).","If sorting by a key, pass the key function as the second argument: karate.sort(list, item => item.name).","Check that the variable holding the list is defined and not lost before the call."],"exampleFix":"// before\nkarate.sort()\n// after\nkarate.sort(items)","handlingStrategy":"type-guard","validationCode":"if (!Array.isArray(items)) { throw new Error('sort() expects a list, got: ' + items) }\nif (items.length === 0) karate.logger.info('sorting empty list')","typeGuard":"function isList(v) { return v != null && Array.isArray(v) }","tryCatchPattern":"try { var sorted = karate.sort(items) } catch (e) { if (('' + e).indexOf('needs at least one argument') >= 0) { throw new Error('sort(): list argument missing — check variable items is defined') } throw e }","preventionTips":["Always pass the list as the first argument to karate.sort()","Check variable definitions before building sort calls in generated/dynamic JS","Remember the key function is optional — omit it for natural ordering rather than passing null positionally"],"tags":["javascript","argument-validation","sorting"],"backgroundTag":"missing-required-argument","analyzedSha":"a22eb90246d958d15a47bf436693d0121ad2812d","analyzedAt":"2026-09-12T09:01:00.220Z","contentChangedAt":"2026-09-12T09:01:00.220Z","schemaVersion":2},"datasetVersion":"2026-09-19T12:17:13.211Z"}