3

Let's say I have an array of objects:

var list = [
       { name: "A", distance: 1},
       { name: "B", distance: 2},
       { name: "C", distance: 3},
       { name: "D", distance: 4},
       { name: "E", distance: 5},
       { name: "F", distance: 6},
       { name: "G", distance: 7},
       { name: "H", distance: 8} 
    ];

if I have another array like this one :

var disturbed = ["G", "B", "C", "F"];

how can I sort disturbed array based on distance property from the list array like this:

["B", "C", "F", "G"];

Edit: I have tried this code with no success:

items = [ 
       { name: "A", distance: 1},
       { name: "B", distance: 2},
       { name: "C", distance: 3},
       { name: "D", distance: 4},
       { name: "E", distance: 5},
       { name: "F", distance: 6},
       { name: "G", distance: 7},
       { name: "H", distance: 8}
]

sorting = [ 1, 2,3,4,5,6,7,8,9,10 ];
result = []
for (let i = 0; i < items.length; i++){
sorting.forEach(function(key) {
    var found = false;
    items = items.filter(function(item) {

        if(!found && items[i].distance == key) {
            result.push(item);
            found = true;
            return false;
        } else 
            return true;
    })
})

result.forEach(function(item) {
    document.writeln(item[i]) 
})
}

How can I Sort an array based on the property value of another array of objects

2
  • What is the expected output? Commented Aug 4, 2019 at 12:08
  • stackoverflow.com/questions/50755037/… Commented Aug 4, 2019 at 12:09

3 Answers 3

4

You can use .reduce() to change the list array to object and then sort based on this object.

Demo:

var list = [
       { name: "A", distance: 1},
       { name: "B", distance: 2},
       { name: "C", distance: 3},
       { name: "D", distance: 4},
       { name: "E", distance: 5},
       { name: "F", distance: 6},
       { name: "G", distance: 7},
       { name: "H", distance: 8} 
    ];
    
var disturbed = ["G", "B", "C", "F"];
    
var sort = list.reduce((acc, cur) => {
  acc[cur.name] = cur.distance;
  return acc;
}, {});

disturbed.sort((a, b) => sort[a] - sort[b]);

console.log(disturbed)

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

Comments

3

You can use the sort method to do that

var list = [
       { name: "A", distance: 1},
       { name: "B", distance: 2},
       { name: "C", distance: 3},
       { name: "D", distance: 4},
       { name: "E", distance: 5},
       { name: "F", distance: 6},
       { name: "G", distance: 7},
       { name: "H", distance: 8} 
    ];

var disturbed = ["G", "B", "C", "F"];

disturbed.sort((a, b) => {
    var itemA = list.find(item => item.name === a);
    var itemB = list.find(item => item.name === b);
    return itemA.distance - itemB.distance;
});

disturbed.forEach(function(item) {
    document.writeln(item[i]) 
})

Comments

3

You can use .find() to find the object with the specified name property that matches your elements in distributed. Once you have got this you can then get the distance property and calculate the difference to sort accordingly:

const list = [
  { name: "A", distance: 1},
  { name: "B", distance: 2},
  { name: "C", distance: 3},
  { name: "D", distance: 4},
  { name: "E", distance: 5},
  { name: "F", distance: 6},
  { name: "G", distance: 7},
  { name: "H", distance: 8} 
];

const disturbed = ["G", "B", "C", "F"];

const res = disturbed.sort((a, b) => {
  const {distance: d_a} = list.find(({name}) => name === a);
  const {distance: d_b} = list.find(({name}) => name === b);
  return d_a - d_b;
});

console.log(res);

A more efficient approach would be to create a new Map using .map() and then use .sort() on the keys form the map:

const list = [
  { name: "A", distance: 1},
  { name: "B", distance: 2},
  { name: "C", distance: 3},
  { name: "D", distance: 4},
  { name: "E", distance: 5},
  { name: "F", distance: 6},
  { name: "G", distance: 7},
  { name: "H", distance: 8} 
];
const disturbed = ["G", "B", "C", "F"];

const lut = new Map(list.map(({name, distance}) => [name, distance]));
const res = disturbed.sort((a, b) => lut.get(a) - lut.get(b));
console.log(res);

2 Comments

.find inside a sort is very inefficient though, something like O(log n² * n²)
@JonasWilms yeah, I agree it's not very efficient. I was going to suggest a better approach but Cuong Le Ngoc provided that

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.