0
var obj = 
[{
    "distance": 7000,
    "rating": 2,
    "name": 'a'
},
{
    "distance": 3470,
    "rating": 1,
    "name": 'b'
}, {
    "distance": 3470,
    "rating": 2,
     "name": 'c'
}, {
    "distance": 3480,
    "rating": 5,
    "name": 'd'
}, {
    "distance": 3490,
    "rating": 3,
    "name": 'e'
}, {
    "distance": 4000,
    "rating": 3,
    "name": 'f'
}];

I am looking to get the output sorted by distance and if distances are nearer to each other by a value (eg. 10), then sort by rating.

I have tried many ways but nothing works.

 obj.sort(function(a, b) {
     return (a["distance"] - b["distance"]);
 });

 obj.sort(function(a, b) {
    return (b["distance"] - a["distance"] <= 10 ? 0: 1) || b["rating"] - 
    a["rating"];
 });

In above code, first i tried to sort by distance and then if distance difference is less than 10, i sort by rating. But this does not works correctly.

JSBin link - https://jsbin.com/nawiderebu/edit?html,js,console

I would like to get output as follows:

[
{
    "distance": 3470,
    "rating": 2,
     "name": 'c'
},{
    "distance": 3470,
    "rating": 1,
    "name": 'b'
}, {
    "distance": 3480,
    "rating": 5,
    "name": 'd'
}, {
    "distance": 3490,
    "rating": 3,
    "name": 'e'
}, {
    "distance": 4000,
    "rating": 3,
    "name": 'f'
},{
    "distance": 7000,
    "rating": 2,
    "name": 'a'
}
]
Sulaiman
  • 61
  • 1
  • 11
  • 1
    Possible duplicate of [How to sort an array of objects by multiple fields?](https://stackoverflow.com/questions/6913512/how-to-sort-an-array-of-objects-by-multiple-fields) – Patrick Roberts Nov 13 '18 at 06:28
  • Would be nice if you can add expected output with given sample data for better understanding. – Hardik Shah Nov 13 '18 at 06:32
  • 1
    Don't you think that as per you requirement expected result should have `"distance": 3480,` object at 0th index? – Karan Nov 13 '18 at 06:38
  • Did you use `arr.sort((a,b) => a.distance - b.distance || b.rating - a.rating);` ? – Hassan Imam Nov 13 '18 at 06:48
  • how do you decide, if an item is closer to a group, if it would fit into two groups? sorting does not work in this context with just taking a distance. you need to make groups and the sort the groups by rating. is distance 3470, 3480 and 3490 the same group? – Nina Scholz Nov 13 '18 at 07:46

2 Answers2

1

Use Math.abs(a["distance"] - b["distance"] < 10) in condition as below. You don't need to have second sort call.

var obj = [
    {
        "distance": 7000,
        "rating": 2,
        "name": 'a'
    },
    {
        "distance": 3470,
        "rating": 1,
        "name": 'b'
    }, {
        "distance": 3470,
        "rating": 2,
         "name": 'c'
    }, {
        "distance": 3480,
        "rating": 5,
        "name": 'd'
    }, {
        "distance": 3490,
        "rating": 3,
        "name": 'e'
    }, {
        "distance": 4000,
        "rating": 3,
        "name": 'f'
    }
]

obj.sort(function(a, b) {
    return Math.abs(a["distance"] - b["distance"]) <= 10 ? (b["rating"] - a["rating"]) : (a["distance"] - b["distance"]);
});

console.log(obj)
Karan
  • 12,059
  • 3
  • 24
  • 40
1

The comparison should return negative, zero, or positive difference (adjust 20 as needed) :

var arr = [ { "distance": 7000, "rating": 2, "name": 'a' }, 
            { "distance": 3470, "rating": 1, "name": 'b' }, 
            { "distance": 3470, "rating": 2, "name": 'c' }, 
            { "distance": 3480, "rating": 5, "name": 'd' }, 
            { "distance": 3490, "rating": 3, "name": 'e' }, 
            { "distance": 4000, "rating": 3, "name": 'f' } ]

arr.sort((a, b) => (a.distance - b.distance) / 20 | 0 || b.rating - a.rating)

console.log( JSON.stringify(arr).replace(/},/g, '},\n ') )
Slai
  • 22,144
  • 5
  • 45
  • 53