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.
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.
Share Improve this question edited Jan 20, 2015 at 0:13 SQRCAT asked Nov 14, 2014 at 2:52 SQRCATSQRCAT 5,8409 gold badges45 silver badges78 bronze badges3 Answers
Reset to default 22Just check their distance from 1.
arr.sort(function(a, b){
return Math.abs(1-a) - Math.abs(1-b);
});
Just to elaborate, it calculates the distance of two numbers from 1, i.e. for
a=-10
andb=4
, the distances are 11 and 3 respectively. The function returns a positive number, so 4 comes before -10 in the sorted array.- For
a=-1
andb=4,
the distances would be 2 and 3, the function returns a negative number so -1 comes before 4 in the array.
As requested in the comments, the below adaptation would give preference to values below 1.
arr.sort(function(a, b){
if(a<1 && b>=1){return -1;}
if(a>=1 && b<1){return 1;}
return (Math.abs(1-a) - Math.abs(1-b));
});
If you want numbers less than 1 to be biased to sort lower that equally distant numbers greater than 1, test if the delta is equal and modify one of the values if it is:
var arr = [1.02, 0.3, 0.76, 0.98, 1.12, 1.36, 1.9, 1.24];
// Unbiased
arr.sort(function(a, b){
return Math.abs(1-a) - Math.abs(1-b);
});
console.log('unbiased: ' + arr); // unbiased: 1.02,0.98,1.12,0.76,1.24,1.36,0.3,1.9
var arr = [1.02, 0.3, 0.76, 0.98, 1.12, 1.36, 1.9, 1.24];
// Biased so numbers less than 1 sort higher than those greater than 1
// where their difference from 1 is equal
arr.sort(function(a, b) {
var da = 1 - a;
var db = 1 - b;
da -= da < 0? 1e-14 : 0;
db -= db < 0? 1e-14 : 0;
return Math.abs(da) - Math.abs(db);
});
console.log('biased: ' + arr); // biased: 0.98,1.02,1.12,0.76,1.24,1.36,0.3,1.9
Just to update @batscream answer with arrow function implementation
arr = arr.sort((a, b) => {
return Math.abs(1-a) - Math.abs(1-b);
});
var arr = [0.3, 0.76, 0.98, 1.12, 1.36, 1.9];
arr = arr.sort((a, b) => {
return Math.abs(1-a) - Math.abs(1-b);
});
console.log(arr);
Reference : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions