I need to sort an array of values.
var arr = [0.3, 0.76, 0.98, 1.12, 1.36, 1.9];
the value of which is closest to 1, which (in the above example) will result in:
[0.98, 1.12, 0.76, 1.36, 0.3, 1.9]
I know using a special sort function.
arr.sort(function(a, b){
return b - a;
});
I can control how it works sort(), but I don’t understand how I could create this user-defined function so that it works in the desired way.
Maybe someone can enlighten me.
source
share