Skip to content

arrayMutableSorts

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 ansty="le--0:#F78C6C;--1:#AA09">8<23sp ansty="le--0:#D6DEEB;--1:#403F">53<, sp ansty="le--0:#F78C6C;--1:#AA09">8<21sp ansty="le--0:#D6DEEB;--1:#403F">53<, sp ansty="le--0:#F78C6C;--1:#AA09">8<22sp ansty="le--0:#D6DEEB;--1:#403F">53<];d ivclassNa="meec-li">AEconsB0sortAE <= sp ansty="le--0:#D6DEEB;--1:#403F">53valuAB0so53(<);d ivclassNa="meco"><);d>AEconsB0iteAE< =sp ansty="le--0:#D6DEEB;--1:#403F">53< [sp ansty="le--0:#F78C6C;--1:#AA09">8<23sp ansty="le--0:#D6DEEB;--1:#403F">53<, sp ansty="le--0:#F78C6C;--1:#AA09">8<21sp ansty="le--0:#D6DEEB;--1:#403F">53<, sp ansty="le--0:#F78C6C;--1:#AA09">8<22sp ansty="le--0:#D6DEEB;--1:#403F">53<];d ivclassNa="meec-li">AEconsB0sortAE <= sp ansty="le--0:#D6DEEB;--1:#403F">53iteAB0so5<3(sp ansty="le--0:#D9F5DD;--1:#1111">1<1(sp ansty="le--0:#D7DBE0;--1:#403F">5<3asp ansty="le--0:#C792EA;--1:#8844">AE<, sp ansty="le--0:#D7DBE0;--1:#403F">5<3bsp ansty="le--0:#D9F5DD;--1:#1111">1<1)sp ansty="le--0:#C792EA;--1:#8844">AE =<> sp ansty="le--0:#D6DEEB;--1:#403F">5 sp><3asp ansty="le--0:#C792EA;--1:#8844">AE <- sp ansty="le--0:#D6DEEB;--1:#403F">53b<);d ivclassNa="meco"><);d>AEconsB0daAE< =sp ansty="le--0:#D6DEEB;--1:#403F">53< [sp ansty="le--0:#F78C6C;--1:#AA09">8<23sp ansty="le--0:#D6DEEB;--1:#403F">53<, sp ansty="le--0:#F78C6C;--1:#AA09">8<21sp ansty="le--0:#D6DEEB;--1:#403F">53<, sp ansty="le--0:#F78C6C;--1:#AA09">8<22sp ansty="le--0:#D6DEEB;--1:#403F">53<];d ivclassNa="meec-li">AEconsB0resuAE< =sp ansty="le--0:#D6DEEB;--1:#403F">53< [sp ansty="le--0:#7FDBCA;--1:#096E">72.<..sp ansty="le--0:#D6DEEB;--1:#403F">53datAB0so53(<);d ivclassNa="meco"><);d>
44AEco61B0va443F<53 [ spans="tyle--0:#F78C6C;--1:#AA">0<9823 spans="tyle--0:#D6DEEB;--1:#40">3F<53, spans="tyle--0:#F78C6C;--1:#AA">0<9821 spans="tyle--0:#D6DEEB;--1:#40">3F<53, spans="tyle--0:#F78C6C;--1:#AA">0<9822 spans="tyle--0:#D6DEEB;--1:#40">3F<53] ;divclass="Nameec-">44AEco61B0so44A3F53va4<4AE. spans="tyle--0:#82AAFF;--1:#3B">61B0toSo3F5<3() ;divclass="Name"><;di vdivclass="Nameexpressive-">44AEco61B0i443F<53 [ spans="tyle--0:#F78C6C;--1:#AA">0<9823 spans="tyle--0:#D6DEEB;--1:#40">3F<53, spans="tyle--0:#F78C6C;--1:#AA">0<9821 spans="tyle--0:#D6DEEB;--1:#40">3F<53, spans="tyle--0:#F78C6C;--1:#AA">0<9822 spans="tyle--0:#D6DEEB;--1:#40">3F<53] ;divclass="Nameec-">44AEco61B0so44A3F53i4<4AE. spans="tyle--0:#82AAFF;--1:#3B">61B0toSo31<111( spans="tyle--0:#D7DBE0;--1:#40">34431<111) spans="tyle--0:#C792EA;--1:#88">44AE< => spans="tyle--0:#D6DEEB;--1:#40">3 >44A3F5<3b) ;divclass="Name">< b)><;di vdivclass="Nameexpressive-">44AEco61B0va443F<53 [ spans="tyle--0:#F78C6C;--1:#AA">0<9823 spans="tyle--0:#D6DEEB;--1:#40">3F<53, spans="tyle--0:#F78C6C;--1:#AA">0<9821 spans="tyle--0:#D6DEEB;--1:#40">3F<53, spans="tyle--0:#F78C6C;--1:#AA">0<9822 spans="tyle--0:#D6DEEB;--1:#40">3F<53] ;divclass="Nameec-">3F53va4<4AE. spans="tyle--0:#82AAFF;--1:#3B">61B03F5<3() ;divclass="Name">

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.