{
    "mode": "perldoc",
    "parameter": "sort",
    "section": "",
    "url": "https://www.chedong.com/phpMan.php/perldoc/sort/json",
    "generated": "2026-06-16T10:24:19Z",
    "synopsis": "use sort 'stable';          # guarantee stability\nuse sort 'defaults';        # revert to default behavior\nno  sort 'stable';          # stability not important\nmy $current;\nBEGIN {\n$current = sort::current();     # identify prevailing pragmata\n}",
    "sections": {
        "NAME": {
            "content": "sort - perl pragma to control sort() behaviour\n",
            "subsections": []
        },
        "SYNOPSIS": {
            "content": "use sort 'stable';          # guarantee stability\nuse sort 'defaults';        # revert to default behavior\nno  sort 'stable';          # stability not important\n\nmy $current;\nBEGIN {\n$current = sort::current();     # identify prevailing pragmata\n}\n",
            "subsections": []
        },
        "DESCRIPTION": {
            "content": "With the \"sort\" pragma you can control the behaviour of the builtin\n\"sort()\" function.\n\nA stable sort means that for records that compare equal, the original\ninput ordering is preserved. Stability will matter only if elements that\ncompare equal can be distinguished in some other way. That means that\nsimple numerical and lexical sorts do not profit from stability, since\nequal elements are indistinguishable. However, with a comparison such as\n\n{ substr($a, 0, 3) cmp substr($b, 0, 3) }\n\nstability might matter because elements that compare equal on the first\n3 characters may be distinguished based on subsequent characters.\n\nWhether sorting is stable by default is an accident of implementation\nthat can change (and has changed) between Perl versions. If stability is\nimportant, be sure to say so with a\n\nuse sort 'stable';\n\nThe \"no sort\" pragma doesn't *forbid* what follows, it just leaves the\nchoice open. Thus, after\n\nno sort 'stable';\n\nsorting may happen to be stable anyway.\n",
            "subsections": []
        },
        "CAVEATS": {
            "content": "As of Perl 5.10, this pragma is lexically scoped and takes effect at\ncompile time. In earlier versions its effect was global and took effect\nat run-time; the documentation suggested using \"eval()\" to change the\nbehaviour:\n\n{ eval 'no sort \"stable\"';      # stability not wanted\nprint sort::current . \"\\n\";\n@a = sort @b;\neval 'use sort \"defaults\"';   # clean up, for others\n}\n{ eval 'use sort qw(defaults stable)';     # force stability\nprint sort::current . \"\\n\";\n@c = sort @d;\neval 'use sort \"defaults\"';   # clean up, for others\n}\n\nSuch code no longer has the desired effect, for two reasons. Firstly,\nthe use of \"eval()\" means that the sorting algorithm is not changed\nuntil runtime, by which time it's too late to have any effect. Secondly,\n\"sort::current\" is also called at run-time, when in fact the\ncompile-time value of \"sort::current\" is the one that matters.\n\nSo now this code would be written:\n\n{ no sort \"stable\";      # stability not wanted\nmy $current;\nBEGIN { $current = sort::current; }\nprint \"$current\\n\";\n@a = sort @b;\n# Pragmas go out of scope at the end of the block\n}\n{ use sort qw(defaults stable);     # force stability\nmy $current;\nBEGIN { $current = sort::current; }\nprint \"$current\\n\";\n@c = sort @d;\n}\n",
            "subsections": []
        }
    },
    "summary": "sort - perl pragma to control sort() behaviour",
    "flags": [],
    "examples": [],
    "see_also": []
}