karatelabs/karate · error · RuntimeException

sort() needs at least one argument: the list to sort

Error message

sort() needs at least one argument: the list to sort

What it means

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.

Solutions

  1. Pass the list as the first argument: karate.sort(myList).
  2. If sorting by a key, pass the key function as the second argument: karate.sort(list, item => item.name).
  3. Check that the variable holding the list is defined and not lost before the call.

Example fix

// before
karate.sort()
// after
karate.sort(items)
Defensive patterns

Strategy: type-guard

Validate before calling

if (!Array.isArray(items)) { throw new Error('sort() expects a list, got: ' + items) }
if (items.length === 0) karate.logger.info('sorting empty list')

Type guard

function isList(v) { return v != null && Array.isArray(v) }

Try / catch

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 }

Prevention

When it happens

Trigger: 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().

Common situations: 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.

Understand the failure class

Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.

Related errors


AI-assisted analysis of karatelabs/karate@a22eb90246 (2026-09-12). Data as JSON: /api/errors/85daf6a74455a22a. Report an issue: GitHub.

Appendix: source

Thrown at karate-core/src/main/java/io/karatelabs/core/KarateJsUtils.java:484

            if (args.length == 0) {
                throw new RuntimeException("sizeOf() needs one argument");
            }
            Object obj = args[0];
            if (obj instanceof List) {
                return ((List<?>) obj).size();
            } else if (obj instanceof Map) {
                return ((Map<?, ?>) obj).size();
            } else if (obj instanceof String) {
                return ((String) obj).length();
            }
            return 0;
        };
    }

    static JavaInvokable sort() {
        return args -> {
            if (args.length == 0) {
                throw new RuntimeException("sort() needs at least one argument: the list to sort");
            }
            List<?> list = (List<?>) args[0];
            // the key function is optional, without it items are compared as-is (natural ordering)
            JavaCallable fn = args.length > 1 && args[1] instanceof JavaCallable ? (JavaCallable) args[1] : null;
            int count = list.size();
            // extract keys once up front instead of on every comparison
            List<Object[]> pairs = new ArrayList<>(count);
            for (int i = 0; i < count; i++) {
                Object item = list.get(i);
                Object key = fn == null ? item : fn.call(null, new Object[]{item, i});
                pairs.add(new Object[]{key, item});
            }
            pairs.sort((a, b) -> compareKeys(a[0], b[0]));
            List<Object> result = new ArrayList<>(count);
            for (Object[] pair : pairs) {
                result.add(pair[1]);
            }
            return result;

View on GitHub (pinned to a22eb90246)