DEV Community

Discussion on: Sorting Array In Different Languages JavaScript, Ruby, Python

Collapse
 
frankwisniewski profile image
Frank Wisniewski • Edited

Your first JS Sample has a wrong Array Declaration

let myArr = [ 4,2,5,7,3,8,1 ]
myArr.sort( ( a, b ) => a-b )
console.log (myArr )
Enter fullscreen mode Exit fullscreen mode

The second example is bad because it doesn't take local circumstances

better:

let string_arr = ['ad', 'ds', 'ar', 'äd' ,'ee']
string_arr.sort( ( a, b )  => a.localeCompare( b ) );
console.log(string_arr);
Enter fullscreen mode Exit fullscreen mode