I need to sort an array of values.
var arr = [0.3, 0.76, 0.98, 1.12, 1.36, 1.9];
by which value is closest to 1
, which would (in the above example) result in:
[0.98, 1.12, 0.76, 1.36, 0.3, 1.9];
I know that by using a custom sort function.
arr.sort(function(a, b){
return b - a;
});
i can take control of how sort()
works, however, i do not understand how i would design that custom function so that it would work in the desired way.
Perhaps someone can enlighten me.