Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

javascript: array sort data become weird

previously i’ve do some technical test with hackerrank. For the simple testing i need to make two different array so i can check it the difference both of them. the first array will be unsorted, and the second will be sorted.

Here’s my code:

function dataSort(thedata) {
    // Write your code here
    var unsorted = thedata
    var sorted = thedata
    console.log("not sorted", unsorted) // first log
    for(let i = 0; i < sorted.length; i++)
    {
        for(let j = i; j < sorted.length; j++)
        {
            if(sorted[i] > sorted[j])
            {
                let temp = sorted[i]
                sorted[i] = sorted[j]
                sorted[j] = temp
                
            }
        }
    }
    console.log("sorted",sorted) // second log
}
dataSort([1,3,4,2,1])

above code given the result below

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

not sorted [ 1, 3, 4, 2, 1 ]
sorted [ 1, 1, 2, 3, 4 ]

Okay, that’s seems have no problem. but when i move the first console after the bubble sort near with second console, the data also sorted

function dataSort(thedata) {
    // Write your code here
    var unsorted = thedata
    var sorted = thedata
    for(let i = 0; i < sorted.length; i++)
    {
        for(let j = i; j < sorted.length; j++)
        {
            if(sorted[i] > sorted[j])
            {
                let temp = sorted[i]
                sorted[i] = sorted[j]
                sorted[j] = temp
                
            }
        }
    }
    console.log("not sorted", unsorted) // first log
    console.log("sorted",sorted) // second log
}
dataSort([1,3,4,2,1])

result

not sorted [ 1, 1, 2, 3, 4 ]
sorted [ 1, 1, 2, 3, 4 ]

Can you tell me what happened?

>Solution :

GrafiCode gave you the correct explanation about why this happens.

Here is a possible solution:

function dataSort(thedata) {
    // Write your code here
    var unsorted = new Array(...thedata)
    var sorted = new Array(...thedata)
    for(let i = 0; i < sorted.length; i++)
    {
        for(let j = i; j < sorted.length; j++)
        {
            if(sorted[i] > sorted[j])
            {
                let temp = sorted[i]
                sorted[i] = sorted[j]
                sorted[j] = temp
                
            }
        }
    }
    console.log("not sorted", unsorted) // first log
    console.log("sorted",sorted) // second log
}
dataSort([1,3,4,2,1])

I hope it helps you 🙂

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading