I have the below code which is also in this fiddle.
I have listed the output and the expected output below. I can't understand why I haven't sorted the rNums yet it still outputs the sorterd results?
If I comment out the sort function then random appears as I want but not ordered. What am I doing wrong here?
Random should be displaying as it is in nNums and ordered should be displayed in numerical order?
<ul id="random"></ul>
<ul id="ordered"></ul>
<script>
var rNums = [1,5,3,2,4];
console.log(rNums);
var sortRNums = rNums;
sortRNums.sort(function (a, b) {
return a - b
});
$.each(rNums, function (index, value) {
$("#random").append('<li>' + value + '</li>');
});
$.each(sortRNums, function (index, value) {
$("#ordered").append('<li>' + value + '</li>');
});
console.log(sortRNums);
</script>
Output
<ul id="random">
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
</ul>
<ul id="ordered">
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
</ul>
Expected Output
<ul id="random">
<li>1</li>
<li>5</li>
<li>3</li>
<li>2</li>
<li>4</li>
</ul>
<ul id="ordered">
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
</ul>