How to sort an array number without the sort method in a single loop

Here is the below code to see the simple sort example in javascript

function customSort(arr){
      
      for(var i=0;i<arr.length-1;i++){
         var num=arr[i];
         var num2=arr[i+1];
        if(num>num2){
         var temp=arr[i];
        arr[i]=arr[i+1];
        arr[i+1]=temp;
        i=0;
       }
     }
}

//call the customSort()

var arr=[5,1,4,3];
console.log(customSort(arr));
Output : 1345

Leave a Reply

Your email address will not be published. Required fields are marked *