Skip to content Skip to sidebar Skip to footer

Sorting Array By Predefined Number

Let's say I have multidimensional array var arr = [{ 'id': '1', 'firstname': 'SUSAN', 'dezibel': '91' }, { 'id': '2', 'firstname': 'JOHNNY', 'dezibel': '7

Solution 1:

You'll need to use a custom sort function that compares the absolute difference of each object's dezibel attribute from 78.

var arr = [{
    "id": "1",
    "firstname": "SUSAN",
    "dezibel": "91"
}, {
    "id": "2",
    "firstname": "JOHNNY",
    "dezibel": "74"
}, {
    "id": "3",
    "firstname": "ANDREA",
    "dezibel": "67"
}];

num = 78;

arr.sort(
  function(first,second){
    var a = Math.abs(num - (+first.dezibel));
    var b = Math.abs(num - (+second.dezibel));
    return a - b;
  });

alert(JSON.stringify(arr));

Solution 2:

Write a sort function which calculates the distance to your number:

arr.sort(function(a, b){
    return Math.abs(num-a) - Math.abs(num-b);
});

Use this to sort the dezibel properties in your array. It will calculate the distance between each of them and num. It will then select the smaller of the two distances, and continue in this manner to sort the whole array.

Solution 3:

Just sort by the absolute difference.

var arr = [{ "id": "1", "firstname": "SUSAN", "dezibel": "91" }, { "id": "2", "firstname": "JOHNNY", "dezibel": "74" }, { "id": "3", "firstname": "ANDREA", "dezibel": "67" }],
    num = 78;

arr.sort(function (a, b) {
    returnMath.abs(a.dezibel - num) - Math.abs(b.dezibel - num);
});
document.write('<pre>' + JSON.stringify(arr, 0, 4) + '</pre>');

Solution 4:

.sort optionally takes a function. The function takes 2 values at a time, and compares them:

  • If the first value should sort higher than the second, the function should return a positive number.
  • If the first value should sort lower than the second, the function should return a negative number.
  • If the values are equal, the function should returns 0.

So, if you wanted to sort by dezibel in ascending order, you could do

arr.sort(function(a,b){
    return a.dezibel-b.dezibel;
});

However, you want to sort by dezibel's distance from some number. To find the magnitude of the difference from 78 and the dezibel value, take the absolute value of the difference:

Math.abs(78 - a.dezibel)

Now, if we want to sort based on that value for each object, we can take the difference of that Math.abs call for both a and b:

arr.sort(function(a,b){
    return Math.abs(78 - a.dezibel) - Math.abs(78 - b.dezibel);
});

Solution 5:

You can use the array sort function for this:

arr.sort(function(a, b) {
   return num - 1 * a.dezibel + num - 1 * b.dezibel
})

Post a Comment for "Sorting Array By Predefined Number"