-4

i need to sort the array of people according to who has the most interests in common with Sharon. The person with the most interests matching should be on top. getSortedList() function that sorts the lists of people - whoever has the most similar interests with the 'sharon' object (3 matching interests) should be on top

const interestList = [
  "gaming",
  "driving",
  "football",
  "fishing",
  "painting",
  "cooking",
  "singing",
  "shopping",
  "running",
  "clubbing"
];

const people = [
  { name: "Ahmad", interests: ["shopping", "painting", "cooking"] },
  { name: "Betty", interests: ["running", "painting", "football"] },
  { name: "Charlie", interests: ["gaming", "football", "painting"] },
  { name: "Diana", interests: ["fishing", "singing", "driving"] },
  { name: "Ethan", interests: ["gaming", "clubbing", "cooking"] },
  { name: "Farhan", interests: ["cooking", "driving", "fishing"] },
  { name: "Gwen", interests: ["singing", "fishing", "gaming"] },
  { name: "Helen", interests: ["football", "clubbing", "shopping"] },
  { name: "Imelda", interests: ["painting", "running", "football"] },
  { name: "Josef", interests: ["shopping", "running", "cooking"] },
  { name: "Khan", interests: ["fishing", "running", "clubbing"] },
  { name: "Lionel", interests: ["gaming", "singing", "driving"] }
];

const sharon = {
  name: "Sharon",
  interests: ["football", "painting", "gaming"]
};

function getSortedList() {
  let output = people.slice();

  return person;
}

function printPeople() {
  let list = getSortedList();
  list.unshift(sharon);
  list.forEach(person => {
    person.interest1 = person.interests[0];
    person.interest2 = person.interests[1];
    person.interest3 = person.interests[2];
    delete person.interests;
  });
  console.log("Friend Matching Script Output:");
  console.table(list);
  console.table(getSortedList());
}

printPeople();
Husein
  • 41
  • 1
  • 7
  • 9
    see where it says `Write your code below` - that's where you write your code, then, if your code doesn't work, you can ask why ... SO is not a code writing service and expects you to attempt to resolve your problem first – Jaromanda X Jul 20 '19 at 00:38
  • @JaromandaX im sorry man, i'm new in programming and stackoverflow. i just don't know the logic, i've been trying looping ways and push it into array. but then....i'm confused, how to compare it into other array object. – Husein Jul 20 '19 at 00:51

3 Answers3

0

You can try something like this. If you need to modify your sort to beyond just the count, you can tweak the sort method.

let people = [
  { name: "Ahmad", interests: ["shopping", "painting", "cooking"] },
  { name: "Betty", interests: ["running", "painting", "football"] },
  { name: "Charlie", interests: ["gaming", "football", "painting"] },
  { name: "Diana", interests: ["fishing", "singing", "driving"] },
  { name: "Ethan", interests: ["gaming", "clubbing", "cooking"] },
  { name: "Farhan", interests: ["cooking", "driving", "fishing"] },
  { name: "Gwen", interests: ["singing", "fishing", "gaming"] },
  { name: "Helen", interests: ["football", "clubbing", "shopping"] },
  { name: "Imelda", interests: ["painting", "running", "football"] },
  { name: "Josef", interests: ["shopping", "running", "cooking"] },
  { name: "Khan", interests: ["fishing", "running", "clubbing"] },
  { name: "Lionel", interests: ["gaming", "singing", "driving"] }
];

const sharon = {
  name: "Sharon",
  interests: ["football", "painting", "gaming"]
};

people.forEach((person, index)=> {
  let count = 0;
  person.interests.forEach(int => {
  sharon.interests.forEach(interest => {

    if(int === interest) {
      count++
      people[index]['count'] = count;
    }
  })
  })
})

people.sort((a,b) => {
  if(a.count >= b.count) {
    return -1;
  } else {
    return 1;
  }
})
James
  • 201
  • 2
  • 7
  • 1
    Did you try this code? It looks like you are declaring `count` *inside* the inner loop, which means that it gets rewritten each iteration, it will never take a value above 1. – Raul Sauco Jul 20 '19 at 01:00
  • Great catch Raul. That would be an issue. I'll update the code to move the count under the people loop. – James Jul 20 '19 at 01:04
0

Usually, the easiest way to sort something is when they are in number form. Currently, you only have data in the form of arrays and objects, which is difficult to sort by itself. Instead, as you want to sort by the number of common interests between two people, we can use this number to help us sort your array of objects.

Thus, the first step to solving your problem is to figure out a way to find the number of "overlapping" interests one object has with another. This can be done by using a method such as this:

const get_overlapping = interests =>
  sharon.interests.filter(hobby => interests.includes(hobby)).length;

The above method will loop through sharon's interests (using .filter()), and only keep those which appear (.includes()) in the passed in interests array. As .filter returns an array of overlapping interests with sharon's, we can then simply get the .length of this array to find the number of overlapping interests.

The next step is to now use our method of get_overlapping to sort the list. Javascript provides a .sort() method, where we can supply a function to it which defines how it should sort.

If the supplied function (which has two parameters a and b, both representing elements in your array) returns a number less than 0, then it will make a come before b in the array, if the number returned is 0 then a and b will stay and if the number returned is greater than 0 then b will come before a. Using this idea, we can sort the people array by using the following:

get_overlapping(b) - get_overlapping(a)

where b now represents the interests array of one object in the array, and a represents the interests array of another object in your array

Using this idea, we can use this in your getSortedList method:

const people = [
  { name: "Ahmad", interests: ["shopping", "painting", "cooking"]},
  { name: "Betty", interests: ["running", "painting", "football"] },
  { name: "Charlie", interests: ["gaming", "football", "painting"] },
  { name: "Diana", interests: ["fishing", "singing", "driving"] },
  { name: "Ethan", interests: ["gaming", "clubbing", "cooking"] },
  { name: "Farhan", interests: ["cooking", "driving", "fishing"] },
  { name: "Gwen", interests: ["singing", "fishing", "gaming"] },
  { name: "Helen", interests: ["football", "clubbing", "shopping"] },
  { name: "Imelda", interests: ["painting", "running", "football"] },
  { name: "Josef", interests: ["shopping", "running", "cooking"] },
  { name: "Khan", interests: ["fishing", "running", "clubbing"] },
  { name: "Lionel", interests: ["gaming", "singing", "driving"] }
];

const sharon = {
  name: "Sharon",
  interests: ["football", "painting", "gaming"]
};


const get_overlapping = interests =>
  sharon.interests.filter(i => interests.includes(i)).length;

const getSortedList = () => {
  const output = people.slice(); // make a copy of people so that we don't modify the people array
  output.sort(({interests: a}, {interests: b}) => get_overlapping(b) - get_overlapping(a));
  return output; // return sorted array
}

console.log(getSortedList());

If you are looking to implement a sorting method yourself, there are plenty of sorting algorithms out there which you can take a shot at writing. Some are easier to implement and understand than others (eg: selection sort, insertion sort, etc...), however, they may not be as efficient to implement.

Nick Parsons
  • 45,728
  • 6
  • 46
  • 64
0

Determine count of overlapping interests, then apply sort.

people.map(item => {
    let count = 0; 
    sharon.interests.forEach(interest => {if (item.interests.lastIndexOf(interest) > -1) ++count}); 
    item.counts = count;
    return item;
}).sort((a,b) => a.counts < b.counts ? 1 : (a.counts > b.counts ? -1 : 0))

enter image description here

Murwa
  • 2,238
  • 1
  • 20
  • 21