0
var sortedSquares = function(nums) {
    const squaredArray = []
    for(let i=0; i<nums.length; i++){
      squaredArray.push(Math.pow(nums[i], 2))
    }
    let arrayWithoutZeros = squaredArray.filter(ele => ele!=0 && ele !=1)
    let arrayWithZeros = squaredArray.filter(ele => ele === 0 )
    let arrayWithOnes = squaredArray.filter(ele => ele === 1)
    let finalArray = arrayWithoutZeros.sort().reverse()
    finalArray.unshift(...arrayWithOnes)
    finalArray.unshift(...arrayWithZeros)
    return finalArray
};

const nums = [-4,1,0,3,10]
sortedSquares(nums)

Output: [ 0, 1, 9, 16, 100 ]

So when i call the above function, I thought alright 0's and 1's end up in the end of array , well i wrote an array without it

But when writing this question i realized

var sortedSquares = function(nums) {
    const squaredArray = []
    for(let i=0; i<nums.length; i++){
      squaredArray.push(Math.pow(nums[i], 2))
    }
    let arrayWithoutZeros = squaredArray.filter(ele => ele!=0 && ele !=1)
    let arrayWithZeros = squaredArray.filter(ele => ele === 0 )
    let arrayWithOnes = squaredArray.filter(ele => ele === 1)
    let finalArray = arrayWithoutZeros.sort()
    finalArray.unshift(...arrayWithOnes)
    finalArray.unshift(...arrayWithZeros)
    return finalArray
};

const nums = [-4,1,0,3,10]
sortedSquares(nums)

Output : [ 0, 1, 100, 16, 9 ]

Even sort doesn't return the desired output

What is the reason behind this, Is this because arrays are Objects in Javascript

2
  • You should better read what unshift does. It adds to the beginning of the array. Commented Aug 9, 2021 at 8:50
  • 2
    Is there any reason why sortedSquares() does all the steps it does? Why do you extract the 0s and 1s after squaring all the elements, then sort the other elements, and then prepend the 0s and 1s again? Just square all elements and sort them o.O - [-4,1,0,3,10].map(x => Math.pow(x, 2)).sort((a, b) => Math.sign(b - a)) Commented Aug 9, 2021 at 8:51

1 Answer 1

2

The reason for this is that numbers are getting sorted as strings and NOT according to the actual number value. Therefore 100 is placed before 16 in the sorted array. (The 0s in 100 have have priority compared to the 6 in 16)

The sort() method sorts the elements of an array in place and returns the sorted array. The default sort order is ascending, built upon converting the elements into strings, then comparing their sequences of UTF-16 code units values.

Array.prototype.sort()

Simplified demonstration:

const arr = [1, 2, 3, 9, 16, 100, ];

console.log(arr.sort()); //[1, 100, 16, 2, 3, 9]

const arr = [1, 2, 3, 9, 16, 100];

console.log(arr.sort());

This is how you could sort by number values:

const arr = [1, 2, 3, 9, 16, 100, ];

console.log(arr.sort((a, b) => a -b)); //[1, 2, 3, 9, 16, 100]

const arr = [1, 2, 3, 9, 16, 100];

console.log(arr.sort((a, b) => a - b));

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.