Sorting an Array without modifying the original

Tell us what’s happening:
Hello, I just am not sure why this code wouldn’t be acceptable… any insight is much appreciated :slight_smile:

  **Your code so far**

const globalArray = [5, 6, 3, 2, 9];

function nonMutatingSort(arr) {
// Only change code below this line
let newArr = [];
newArr = arr.slice();
newArr.sort();
return newArr;
// Only change code above this line
}

nonMutatingSort(globalArray);
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36

Challenge: Return a Sorted Array Without Changing the Original Array

Link to the challenge:

the array elements are converted to strings, then sorted according to each character’s Unicode code point value.

In other words, by default sort() will sort the array alphabetically. This is not what you want when you are sorting numbers.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.