Let\'s suppose I wanted a sort function that returns a sorted copy of the inputted array. I naively tried this
function sort(arr) { return arr.sort(); } >
Anyone who wants to do a deep copy (e.g. if your array contains objects) can use:
let arrCopy = JSON.parse(JSON.stringify(arr))
Then you can sort arrCopy without changing arr.
arrCopy
arr
arrCopy.sort((obj1, obj2) => obj1.id > obj2.id)
Please note: this can be slow for very large arrays.