4
I need to sort an array, but I can’t miss the original sequence.
To do this, I created a new variable and assigned the value to it, making a copy of the original. However, when using the method sort
, realized that it modifies the two variables. Follows the code excerpt:
var rank = [292, 130, 55, 232, 213, 62, 152], rankOdered = [];
rankOrdered = rank;
console.log("rankOdered: " + rankOrdered)
console.log("rank: " + rank)
console.log("")
rankOrdered.sort(function (a, b) { return b - a });
console.log("rankOdered: " + rankOrdered)
console.log("rank: " + rank)
On the console screen:
rankOdered: 292,130,55,232,213,62,152
rank: 292,130,55,232,213,62,152
<empty string>
rankOdered: 292,232,213,152,130,62,55
rank: 292,232,213,152,130,62,55
I usually call the function
.splice()
before.sort()
– Denis Rudnei de Souza