How can you sort an array without mutating the original array?
You need to copy the array before you sort it. One way with es6:
const sorted = [...arr].sort();
The spread-syntax as array literal (copied from mdn):
var arr = [1, 2, 3];
var arr2 = [...arr]; // like arr.slice()
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_operator
How to sort an array without destroying the original array?
int[] originalArray = {1,5,6,8,4,2};
int[] backup = Arrays.copyOf(originalArray,originalArray.length);
Arrays.sort(backup);
After the execution of the code above, backup
becomes sorted and originalArray
stays same.
Does .sort function change original array?
Use slice()
to sort a copy of the original array.
var arr =[{time:4},{time:3},{time:6}];
arr.sort(function (a, b) {
return a.time-b.time;
});
will mutate the original array and
returns :
[ { time: 3 }, { time: 4 }, { time: 6 } ]
and console.log(arr) returns
[ { time: 3 }, { time: 4 }, { time: 6 } ]
but
var arr =[{time:4},{time:3},{time:6}];
arr.slice().sort(function (a, b) {
return a.time-b.time;
});
returns
[ { time: 3 }, { time: 4 }, { time: 6 } ]
but will not affect the original array.
console.log(arr) returns
[ { time: 4 }, { time: 3 }, { time: 6 } ]
How to sort a list without mutating it
If you are using Java 8 or higher, you can use the Stream API. See Sorting a list with stream.sorted() in Java for details.
For example:
List<String> sortedList = myList.stream().sorted().collect(Collectors.toList());
Swap objects in the array without mutating the array
without mutating you mean create a copy? Then like this.
const {activeElementIndex, swapedElementIndex} = action.payload
return {
...state,
stages: (stages => {
[stages[activeElementIndex], stages[swapedElementIndex]] =
[stages[swapedElementIndex], stages[activeElementIndex]];
return stages })([...state.stages])
}
(If you want to save an old then just replace [...state.stages] in my code with state.stages)
Reverse array in Javascript without mutating original array
You can use slice() to make a copy then reverse() it
var newarray = array.slice().reverse();
var array = ['a', 'b', 'c', 'd', 'e'];var newarray = array.slice().reverse();
console.log('a', array);console.log('na', newarray);
Related Topics
What Does $.When.Apply($, Somearray) Do
Calling Setstate in a Loop Only Updates State 1 Time
Var Name Produces Strange Result in JavaScript
How to Clone a JavaScript Object Except for One Key
Setting Multiple Attributes for an Element at Once with JavaScript
JavaScript Regular Expressions and Sub-Matches
Classical Inheritance VS Prototypal Inheritance in JavaScript
Node.Js Tail-Call Optimization: Possible or Not
Can You Force Vue.Js to Reload/Re-Render
How to Check Whether an Object Is a Date
How to Determine If an Image Has Loaded, Using JavaScript/Jquery
How Is JavaScript Single Threaded
Use $Http Inside Custom Provider in App Config, Angular.Js
Merge Two Arrays So That the Values Alternate
How to Implement Custom Sort to a Specific Column After Jqgrid Has Been Generated