@Override
    public void run() {
        try { // make sure we call afterRun() even on crashes
            // and operate countdown latches, else we may hang the parallel runner
            if (steps == null) {
                beforeRun();
            }
            if (skipped) {
                return;
            }
            int count = steps.size();
            int index = 0;
            while ((index = nextStepIndex()) < count) {
                currentStep = steps.get(index);
                execute(currentStep);
                if (currentStepResult != null) { // can be null if debug step-back or hook skip
                    result.addStepResult(currentStepResult);
                }
            }
        } catch (Exception e) {
            if (currentStepResult != null) {
                result.addStepResult(currentStepResult);
            }
            logError("scenario [run] failed\n" + StringUtils.throwableToString(e));
            currentStepResult = result.addFakeStepResult("scenario [run] failed", e);
        } finally {
            if (!skipped) {
                afterRun();
                if (isFailed() && engine.getConfig().isAbortSuiteOnFailure()) {
                    featureRuntime.suite.abort();
                }
            }
            if (caller.isNone()) {
                logAppender.close(); // reclaim memory
            }
        }
    }
@Test
    void testSort() {
        run(
                "def list1 = [{ num: 3 }, { num: 1 }, { num: 2 }]",
                "def fun1 = x => x.num",
                "def res1 = karate.sort(list1, fun1)",
                "match res1 == [{ num: 1 }, { num: 2 }, { num: 3 }]",
                "def list2 = [{ val: 'C' }, { val: 'B' }, { val: 'A' }, { val: 'B' }]",
                "def res2 = karate.sort(list2, x => x.val)",
                "match res2 == [{ val: 'A' }, { val: 'B' }, { val: 'B' }, { val: 'C' }]",
                "def list3 = ['c', 'b', 'a']",
                "def res3 = karate.sort(list3)",
                "match res3 == ['a', 'b', 'c']",
                "match res3.reverse() == ['c', 'b', 'a']",
                "def list4 = ['a', 'a a', 'a']",
                "match karate.sort(list4) == ['a', 'a', 'a a']"
        );
    }