0

Let's say I have multidimensional array

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

How can I sort it by "dezibel" but not ascending or descending, but closest to a giving number? For example,

var num = 78;

so target value is 78. and final sorting must be: 74, 67, 91.

1
  • but dezibel is logarithmic value, maybe the sorting should respect that? Commented Jan 6, 2016 at 17:10

6 Answers 6

2

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));

Sign up to request clarification or add additional context in comments.

Comments

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.

2 Comments

This will need to access the properties of the objects in the array, not just the array elements.
I was simply showing the sort function, I will edit my text to be more clear. Thanks!
1

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) {
    return Math.abs(a.dezibel - num) - Math.abs(b.dezibel - num);
});
document.write('<pre>' + JSON.stringify(arr, 0, 4) + '</pre>');

Comments

0

.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);
});

1 Comment

Thank you all for the help. I will accept this as an answer because it's the most detailed response.
0

You can use the array sort function for this:

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

Comments

0

I would just add a distance and then sort it ...

num=78
for e in arr
    e.dist=Math.abs(e.dezibel - num)
arr.sort (l,r) =>
    if l.dist > r.dist then 1 else -1

delete the dist after

but put in one line is ok, too

    arr.sort (l,r) =>
        if Math.abs(l.dezibel - num) > Math.abs(r.dezibel - num) then 1 else -1

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.