How To Sort An Array Without Mutating The Original Array In JavaScript?
Last Updated :
16 Aug, 2024
Sorting arrays is a common task in JavaScript, especially when you need to present data in a specific order. However, maintaining the original array is often necessary especially if the original data will be required later in your code. In this article we'll see how we can sort an array in JavaScript while keeping the original array unmodified.
The sort() Method:
JavaScript provides a built-in sort() method to arrange the elements of an array. However, it's important to note that this method sorts the array in place, meaning it modifies the original array. This behavior might not be ideal, especially when working with immutable data or functional programming principles. To address this, you can create a copy of the array and sort that copy instead. Below are several methods to achieve this:
These are some approach to sort an array without mutating the original array :
1. Using the slice() Method
The method slice() returns a shallow copy of a section of an array into a new array object. You can create a copy that is sortable without changing the original array by applying slice() to the entire array.
Syntax
let sortedArray = array.slice().sort(compareFunction);
Example
JavaScript
const numbers = [4, 2, 5, 1, 3];
const sortedNumbers = numbers.slice().sort((a, b) => a - b);
console.log(numbers);
console.log(sortedNumbers);
Output
Example Output2. Using the Spread Operator (...)
This spread operator (...) can be used to make a shallow copy of an array. It provides a concise way to duplicate an array, allowing you to sort the copy without altering the original array.
Syntax
let sortedArray = [...array].sort(compareFunction);
Example
JavaScript
const names = ['Zoe', 'Alice', 'Bob'];
const sortedNames = [...names].sort();
console.log(names);
console.log(sortedNames);
Output
Example output3. Using Array.from()
The Array.from() method can also be used to create a copy of an array. This method returns a new array instance from an array-like or iterable object, which you can then sort independently of the original array.
Syntax
let sortedArray = Array.from(array).sort(compareFunction);
Example
JavaScript
const letters = ['d', 'a', 'c', 'b'];
const sortedLetters = Array.from(letters).sort();
console.log(letters);
console.log(sortedLetters);
Output
Example Output
Similar Reads
How to Sort a Multidimensional Array in JavaScript by Date ? Sorting a Multidimensional Array by date consists of ordering the inner arrays based on the date values. This needs to be done by converting the date representation into the proper comparable formats and then applying the sorting function to sort the array in ascending or descending order. Below are
2 min read
How to Sort an Array Based on the Length of Each Element in JavaScript? Imagine you have a list of words or groups of items, and you want to arrange them in order from shortest to longest. This is a pretty common task in JavaScript, especially when working with text or collections of things. By sorting your list in this way, you can make sense of your data and make it e
3 min read
How to sort an array of object by two fields in JavaScript ? We have given an array of objects and the task is to sort the array of elements by 2 fields of the object. There are two methods to solve this problem which are discussed below: Approach 1:First compare the first property, if both are unequal then sort accordingly.If they are equal then do the same
3 min read
How to Sort JSON Array in JavaScript by Value ? Sorting is the process of arranging elements in a specific order. In JavaScript, you can sort a JSON array by value using various methods as listed and discussed below with their practical implementation. Table of Content Using sort() FunctionUsing Array.prototype.reduce()Using Bubble SortUsing sort
3 min read
Sort An Array Of Arrays In JavaScript The following approaches can be used to sort array of arrays in JavaScript.1. Using array.sort() Method- Mostly UsedJS array.sort() is used to sort and update the original array in ascending order.JavaScriptlet arr = [[3, 2], [1, 4], [2, 5], [2, 0]]; arr.sort(); console.log(arr);Output[ [ 1, 4 ], [
2 min read