DEV Community

Discussion on: Move Zeros Code Challenge

Collapse
 
deraw profile image
Dylan Broussard • Edited

Hi!

Nice article, and I liked your explanations! 😄

It was a cool challenge, here's my solution, using the sort method:

const arrayToSort = [1, 9, 8, 4, 0, 0, 2, 7, 0, 6, 0];
arrayToSort.sort((number) => number === 0 ? 1 : -1);
// Result: [1, 9, 8, 4, 2, 7, 6, 0, 0, 0, 0]
Enter fullscreen mode Exit fullscreen mode

But I think it's also good to implement the sorting alogtithm from scratch to better undersand how it's working 😃

Collapse
 
adyngom profile image
Ady Ngom

Hello yes the sort method could be a good first route but if you look closely at the inner working of it, it is definitely not an optimized solution. You could always use benchmarking tools to compare the performance of different solutions.
Cheers