Sort An Array Without Mutating The Initial Array In JavaScript
** TL;DR
This is how to get a sorted copy of an array without changing the original one in JavaScript
js
const initial = ;
const new_thing = Array..;
;
;
results start
** Details
Calling [TODO: Code shorthand span ] directly on an array alters it to have the sorted ordering. For example :
js
const example = ;
example.
results start
That's generally what I'm after but there are times when I need the original ordering to stay intact. The code at the top of the post does that by making a new array from the values in the first array and then sorting off that.
#+NOTES :
- I use this for arrays of strings. Dealing with arrays of object might take more work.