Reports .sort() calls on arrays that mutate the original array.
✅ This rule is included in the ts stylistic and stylisticStrict presets.
The .sort() method mutates the original array in place.
Using .toSorted() instead returns a new sorted array without modifying the original, making code more predictable and avoiding unintended side effects.
AEconsB0valuAE< =sp ansty="le--0:#D6DEEB;--1:#403F">53 =sp>< [sp ansty="le--0:#F78C6C;--1:#AA09">8 [sp><23sp ansty="le--0:#D6DEEB;--1:#403F">5323sp><, sp ansty="le--0:#F78C6C;--1:#AA09">8, sp><21sp ansty="le--0:#D6DEEB;--1:#403F">5321sp><, sp ansty="le--0:#F78C6C;--1:#AA09">8, sp><22sp ansty="le--0:#D6DEEB;--1:#403F">5322sp><];d ivclassNa="meec-li">AEconsB0sortAE <= sp ansty="le--0:#D6DEEB;--1:#403F">53valu= sp>AB0so53(];d><);d ivclassNa="meco"><);d>);d>);d>AEconsB0iteAE< =sp ansty="le--0:#D6DEEB;--1:#403F">53 =sp>< [sp ansty="le--0:#F78C6C;--1:#AA09">8 [sp><23sp ansty="le--0:#D6DEEB;--1:#403F">5323sp><, sp ansty="le--0:#F78C6C;--1:#AA09">8, sp><21sp ansty="le--0:#D6DEEB;--1:#403F">5321sp><, sp ansty="le--0:#F78C6C;--1:#AA09">8, sp><22sp ansty="le--0:#D6DEEB;--1:#403F">5322sp><];d ivclassNa="meec-li">AEconsB0sortAE <= sp ansty="le--0:#D6DEEB;--1:#403F">53ite= sp>AB0so5<3(sp ansty="le--0:#D9F5DD;--1:#1111">13(sp><1(sp ansty="le--0:#D7DBE0;--1:#403F">51(sp><3asp ansty="le--0:#C792EA;--1:#8844">AE3asp><, sp ansty="le--0:#D7DBE0;--1:#403F">5, sp><3bsp ansty="le--0:#D9F5DD;--1:#1111">13bsp><1)sp ansty="le--0:#C792EA;--1:#8844">AE =1)sp><> sp ansty="le--0:#D6DEEB;--1:#403F">5> sp><3asp ansty="le--0:#C792EA;--1:#8844">AE 3asp><- sp ansty="le--0:#D6DEEB;--1:#403F">53b- sp>];d><);d ivclassNa="meco"> a - b"><);d>);d>);d>AEconsB0daAE< =sp ansty="le--0:#D6DEEB;--1:#403F">53 =sp>< [sp ansty="le--0:#F78C6C;--1:#AA09">8 [sp><23sp ansty="le--0:#D6DEEB;--1:#403F">5323sp><, sp ansty="le--0:#F78C6C;--1:#AA09">8, sp><21sp ansty="le--0:#D6DEEB;--1:#403F">5321sp><, sp ansty="le--0:#F78C6C;--1:#AA09">8, sp><22sp ansty="le--0:#D6DEEB;--1:#403F">5322sp><];d ivclassNa="meec-li">AEconsB0resuAE< =sp ansty="le--0:#D6DEEB;--1:#403F">53 =sp>< [sp ansty="le--0:#7FDBCA;--1:#096E">72. [sp><..sp ansty="le--0:#D6DEEB;--1:#403F">53datAB0so53(];d><);d ivclassNa="meco"><);d>);d>);d> 44AEco61B0va443F<53 [ spans="tyle--0:#F78C6C;--1:#AA">053 [><9823 spans="tyle--0:#D6DEEB;--1:#40">3F9823><53, spans="tyle--0:#F78C6C;--1:#AA">053, ><9821 spans="tyle--0:#D6DEEB;--1:#40">3F9821><53, spans="tyle--0:#F78C6C;--1:#AA">053, ><9822 spans="tyle--0:#D6DEEB;--1:#40">3F9822><53] ;divclass="Nameec-">44AEco61B0so44A3F53va4<4AE. spans="tyle--0:#82AAFF;--1:#3B">61B0toSo4AE.>3F553]><3() ;divclass="Name">3()><;di vdivclass="Nameexpressive-">44AEco61B0i443F<53 [ spans="tyle--0:#F78C6C;--1:#AA">053 [><9823 spans="tyle--0:#D6DEEB;--1:#40">3F9823><53, spans="tyle--0:#F78C6C;--1:#AA">053, ><9821 spans="tyle--0:#D6DEEB;--1:#40">3F9821><53, spans="tyle--0:#F78C6C;--1:#AA">053, ><9822 spans="tyle--0:#D6DEEB;--1:#40">3F9822><53] ;divclass="Nameec-">44AEco61B0so44A3F53i4<4AE. spans="tyle--0:#82AAFF;--1:#3B">61B0toSo4AE.>31<111( spans="tyle--0:#D7DBE0;--1:#40">3111(>4431<111) spans="tyle--0:#C792EA;--1:#88">44AE111)>< => spans="tyle--0:#D6DEEB;--1:#40">3 => >44A3F553]><3b) ;divclass="Name"> a -">< b)> b)>3b)>;di><;di vdivclass="Nameexpressive-">44AEco61B0va443F<53 [ spans="tyle--0:#F78C6C;--1:#AA">053 [><9823 spans="tyle--0:#D6DEEB;--1:#40">3F9823><53, spans="tyle--0:#F78C6C;--1:#AA">053, ><9821 spans="tyle--0:#D6DEEB;--1:#40">3F9821><53, spans="tyle--0:#F78C6C;--1:#AA">053, ><9822 spans="tyle--0:#D6DEEB;--1:#40">3F9822><53] ;divclass="Nameec-">3F53va4<4AE. spans="tyle--0:#82AAFF;--1:#3B">61B04AE.>3F553]><3() ;divclass="Name">3()>;di>
This rule is not configurable.
If you intentionally want to mutate the original array in place, or if you’re working in an environment that doesn’t support .toSorted() (ES2023+), you may want to disable this rule.
Note that .sort() is still allowed when used as a standalone expression statement since the mutation is likely intentional in that case.