Sort an Array without Mutation using JavaScript

avatar
Borislav Hadzhiev

Last updated: Mar 4, 2024
4 min

banner

# Sort an Array without Mutation in JavaScript

To sort an array without mutation:

  1. Use the spread syntax (...) to create a shallow copy of the array.
  2. Call the Array.sort() method on the copy.
  3. The sort() method will sort the copy without changing the original.
index.js
// โœ… for array of letters const arr = ['z', 'c', 'a', 'f']; const result = [...arr].sort(); console.log(result); // ๐Ÿ‘‰๏ธ [ 'a', 'c', 'f', 'z' ] console.log(arr); // ๐Ÿ‘‰๏ธ [ 'z', 'c', 'a', 'f' ]

sort array without mutation

The code for this article is available on GitHub

If you need to sort an array of numbers, you have to pass a function to the Array.sort() method.

index.js
// โœ… for array of numbers const arr = [10, 5, 1, 7]; const result = [...arr].sort((a, b) => a - b); console.log(result); // ๐Ÿ‘‰๏ธ [1, 5, 7, 10] console.log(arr); // ๐Ÿ‘‰๏ธ [10, 5, 1, 7]

We used the spread syntax (...) to unpack the values of an array into a new array.

index.js
const a = [1, 2, 3]; const b = [...a]; console.log(b); // ๐Ÿ‘‰๏ธ [1, 2, 3]

The spread syntax (...) allows us to create a shallow copy of the original array, on which we can call the Array.sort() method.

The examples show how to sort numeric and string arrays without mutating the original.

When sorting a numeric array, we have to pass a function to the sort() method.

If you need to sort an array of strings, you can call the sort() method without passing any arguments.

The sort() method takes a function that defines the sort order.

index.js
const arr = [10, 5, 1, 7]; const result = [...arr].sort((a, b) => a - b); console.log(result); // ๐Ÿ‘‰๏ธ [1, 5, 7, 10] console.log(arr); // ๐Ÿ‘‰๏ธ [10, 5, 1, 7]
The code for this article is available on GitHub
If the function parameter is not provided to the sort() method, the array elements get converted to strings and sorted according to their UTF-16 code unit values.

This is not what we want when working with numeric arrays, however, it is exactly what we want when sorting string arrays.

If you have to sort an array without mutating it often, define a reusable function.

index.js
function sortWithoutMutation(arr) { return [...arr].sort(); } const arr = ['c', 'b', 'a']; const result = sortWithoutMutation(['c', 'b', 'a']); console.log(result); // ๐Ÿ‘‰๏ธ [ 'a', 'b', 'c' ] console.log(arr); // ๐Ÿ‘‰๏ธ [ 'c', 'b', 'a' ]

The sortWithoutMutation() function takes an array as a parameter and sorts the array without mutating it.

An alternative, but also very common approach is to use the Array.slice method.

# Sort an Array without Mutation using slice()

This is a three-step process:

  1. Call the slice() method on the array to get a copy.
  2. Call the sort() method on the copied array.
  3. The sort method will sort the copied array, without mutating the original.
index.js
// โœ… for numbers const arr3 = [8, 4, 12]; const result3 = arr3.slice().sort((a, b) => a - b); console.log(result3); // ๐Ÿ‘‰๏ธ [4, 8, 12] console.log(arr3); // ๐Ÿ‘‰๏ธ [8, 4, 12] // โœ… for letters const arr = ['z', 'c', 'a', 'f']; const result = arr.slice().sort(); console.log(result); // ๐Ÿ‘‰๏ธ ['a', 'c', 'f', 'z'] console.log(arr); // ๐Ÿ‘‰๏ธ ['z', 'c', 'a', 'f']

sort array without mutation using slice

The code for this article is available on GitHub

If you're working with a numeric array, pass a function to the sort() method.

index.js
const arr = [8, 4, 12]; const result = arr.slice().sort((a, b) => a - b); console.log(result); // ๐Ÿ‘‰๏ธ [4, 8, 12] console.log(arr); // ๐Ÿ‘‰๏ธ [8, 4, 12]

The Array.slice() method can be used to create a shallow copy of an array, just like the spread syntax (...).

When no parameters are passed to the slice method, it returns a shallow copy of the array.

We can then call the sort() method on the copy to not mutate the original.

# Sort an Array without Mutation using Array.from()

You can also use the Array.from() method to sort an array without changing the original.

index.js
const arr = ['z', 'c', 'a', 'f']; const result = Array.from(arr).sort(); console.log(result); // ๐Ÿ‘‰๏ธ ['a', 'c', 'f', 'z'] console.log(arr); // ๐Ÿ‘‰๏ธ ['z', 'c', 'a', 'f']

sort array without mutation using array from

The code for this article is available on GitHub

The Array.from() method creates a new, shallow-copied array from the provided iterable.

Calling the sort() method on the copied array doesn't change the original.

# Sort an Array without Mutation using Array.concat()

The Array.concat() method can also be used to sort an array without mutating the original.

index.js
const arr = ['z', 'c', 'a', 'f']; const result = arr.concat().sort(); console.log(result); // ๐Ÿ‘‰๏ธ ['a', 'c', 'f', 'z'] console.log(arr); // ๐Ÿ‘‰๏ธ ['z', 'c', 'a', 'f']

sort array without mutation using concat

The code for this article is available on GitHub

When the Array.concat() method is called without any arguments, it returns a shallow copy of the array on which it was called.

# Sort an Array without Mutation using JSON.stringify()

You can also use the JSON.stringify() and JSON.parse() methods to sort an array without a mutation.

index.js
const arr = ['z', 'c', 'a', 'f']; const result = JSON.parse(JSON.stringify(arr)).sort(); console.log(result); // ๐Ÿ‘‰๏ธ ['a', 'c', 'f', 'z'] console.log(arr); // ๐Ÿ‘‰๏ธ ['z', 'c', 'a', 'f']
The code for this article is available on GitHub

The JSON.stringify() method converts a JavaScript value to a JSON string.

The JSON.parse() method parses a JSON string into a JavaScript value.

When we convert the array to a JSON string and parse the string back to an array, we get a completely different array.

The new array has a different reference and is stored in a different location in memory, so we can sort the array without mutating the original.

Which approach you pick is a matter of personal preference. I'd use the spread syntax (...) from the first subheading because I find it quite direct and intuitive.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev