{
    "mode": "perldoc",
    "parameter": "sort",
    "section": "",
    "url": "https://www.chedong.com/phpMan.php/perldoc/sort/json",
    "generated": "2026-07-05T11:43:54Z",
    "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 \"sort()\" function.\n\nA stable sort means that for records that compare equal, the original input ordering is\npreserved. Stability will matter only if elements that compare equal can be distinguished in\nsome other way. That means that simple numerical and lexical sorts do not profit from stability,\nsince equal 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 3 characters may be\ndistinguished based on subsequent characters.\n\nWhether sorting is stable by default is an accident of implementation that can change (and has\nchanged) between Perl versions. If stability is important, 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 choice 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 compile time. In earlier\nversions its effect was global and took effect at run-time; the documentation suggested using\n\"eval()\" to change the behaviour:\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, the use of \"eval()\" means\nthat the sorting algorithm is not changed until runtime, by which time it's too late to have any\neffect. Secondly, \"sort::current\" is also called at run-time, when in fact the compile-time\nvalue 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": []
}