# freeCodeCamp Challenge Guide: Return a Sorted Array Without Changing the Original Array

**URL:** <https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-return-a-sorted-array-without-changing-the-original-array/301237>\
**Category:** freeCodeCamp Contributors\
**Tags:** guide\
**Created:** [August 3, 2019, 5:53am UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-return-a-sorted-array-without-changing-the-original-array/301237 "2019-08-03T05:53:42Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![camperbot](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/camperbot/32/18364_2.png) [@camperbot](https://forum.freecodecamp.org/u/camperbot)\
**Post date:** [August 3, 2019, 5:53am UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-return-a-sorted-array-without-changing-the-original-array/301237/1 "2019-08-03T05:53:42Z")

</div>

# [Return a Sorted Array Without Changing the Original Array](https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/functional-programming/return-a-sorted-array-without-changing-the-original-array)

* * *

## Solutions

> **Solution 1 (Click to Show/Hide)**
>
> ```javascript
> var globalArray = [5, 6, 3, 2, 9];
> function nonMutatingSort(arr) {
> // Add your code below this line
> return [].concat(arr).sort(function(a, b) {
> return a - b;
> });
> // Add your code above this line
> }
> nonMutatingSort(globalArray);
> 
> ```
> 
> #### Code Explanation
> 
> - Firstly concatenate the array taken in as a parameter to a new empty array.
> - Then Use the `sort()` method as seen in the last challenge and create a function to sort the new array in ascending order.

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [January 8, 2025, 3:58pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-return-a-sorted-array-without-changing-the-original-array/301237/2 "2025-01-08T15:58:53Z")

</div>

Closed on January 8, 2025, 3:58pm UTC


