I want to create a JavaScript Function that sorts numbers from maximum to minimum without using the traditional .sort() method
const sortArray= (array)=>{
let newArray = []
let firstArray = array[0]
for(const num in array){
if(num>firstArray){
firstArray = num
newArray.push(firstArray)
}else{
firstArray = firstArray
newArray.push(firstArray)
}
}
return newArray
}
console.log(sortArray([0,10,-1,4,7]))
But the code is not sorting properly. I don’t know I missed or added wrong.
2
Answers
A selection sort algorithm to efficiently find and swap elements, resulting in the array sorted from highest to lowest.
You can create a new sorted array by inserting elements with binary search: