something wrong with sorting algo

Run Settings
LanguageJavaScript
Language Version
Run Command
// how come this logic is wrong const sort =(arr)=>{ for(let pointer1=0;pointer1<arr.length;pointer1++){ const char1=arr[pointer1]; for(let pointer2=pointer1+1;pointer2<arr.length;pointer2++){ // this is the problem let char2=arr[pointer2]; if(char2<char1){ [arr[pointer1],arr[pointer2]]=[arr[pointer2],arr[pointer1]] } } } return arr } /////vs const sortRight =(arr)=>{ for(let pointer1=0;pointer1<arr.length;pointer1++){ const char1=arr[pointer1]; // this cant be here since at vellow we want to always update it for(let pointer2=pointer1+1;pointer2<arr.length;pointer2++){ let char2=arr[pointer2]; console.log('arr[pointer2]:',arr[pointer2],'char2:',char2,'arr[pointer1]:',arr[pointer1],'char1:',char1) if(char2<arr[pointer1]){ // this the one who actually fix the problem [arr[pointer1],arr[pointer2]]=[arr[pointer2],arr[pointer1]] } } } return arr } console.log(sort(['t','a','n']))// some how get it wrong n a t console.log(sortRight(['t','a','n']))// the right one a n t
Editor Settings
Theme
Key bindings
Full width
Lines