0

So i have an array of object that looks like this:

    group: [
0: {id: "16", name: "P1", courseId: "6", mentorId: "1", chatUrl: false, students: {0: 1, 1: 10, 2: 11},…}
1: {id: "17", name: "C1", courseId: "7", mentorId: "3", chatUrl: false, students: {0: 15, 1: 16, 2: 18},…}
2: {id: "22", name: "P2", courseId: "6", mentorId: "1", chatUrl: false, students: {0: 12, 1: 13, 2: 9},…}
3: {id: "23", name: "C2", courseId: "7", mentorId: "3", chatUrl: false, students: {0: 17, 1: 19, 2: 20},…}
4: {id: "24", name: "DEV", courseId: "10", mentorId: "1", chatUrl: false,…}]. 

i'm trying to sort it by mentor name which is property of object in another array that looks like this:

mentor: [
0: {id: "0", firstName: "Daniel",  about: false,…}
1: {id: "1", firstName: "Mark",  aboutl:false,…}
2: {id: "3", firstName: "Eric", about: false,…}
3: {id: "6", firstName: "John", about: false,…} ]

The groups are related to mentors by property mentorId, so i have to compare property mentorId from group array with id from mentor array to get all mentors for groups and then sort the group by those mentors firstName. Is this even possible?

The output should display all the groups sorted alphabeticly by mentor firstname, something like this:

group: [
0: {id: "23", name: "C2", courseId: "7", mentorId: "3", chatUrl: false, students: {0: 17, 1: 19, 2: 20},…}
    1: {id: "17", name: "C1", courseId: "7", mentorId: "3", chatUrl: false, students: {0: 15, 1: 16, 2: 18},…}
    2: {id: "22", name: "P2", courseId: "6", mentorId: "1", chatUrl: false, students: {0: 12, 1: 13, 2: 9},…}
    3: {id: "16", name: "P1", courseId: "6", mentorId: "1", chatUrl: false, students: {0: 1, 1: 10, 2: 11},…}
    4: {id: "24", name: "DEV", courseId: "10", mentorId: "1", chatUrl: false,…}
]
3
  • 1
    how the output will look like? Commented Aug 29, 2020 at 9:16
  • 1
    What do you mean with "sort by mentor name"? Sort it in an ascending order? Or sort it in the order the mentor names appear in the mentor array? Commented Aug 29, 2020 at 9:22
  • I edited the question, it should sort the groups alphabetically by mentors firstName, asscending or descending, its not important Commented Aug 29, 2020 at 9:27

2 Answers 2

1

You could first sort the mentors array by firstName property and then create an object where the key is the id and the value is index of that object in the sorted array. Then you can just use sort method on groups array and sort by the value of mentorId property in order object.

const groups = [{"id":"16","name":"P1","courseId":"6","mentorId":"1","chatUrl":false,"students":{"0":1,"1":10,"2":11}},{"id":"17","name":"C1","courseId":"7","mentorId":"3","chatUrl":false,"students":{"0":15,"1":16,"2":18}},{"id":"22","name":"P2","courseId":"6","mentorId":"1","chatUrl":false,"students":{"0":12,"1":13,"2":9}},{"id":"23","name":"C2","courseId":"7","mentorId":"3","chatUrl":false,"students":{"0":17,"1":19,"2":20}},{"id":"24","name":"DEV","courseId":"10","mentorId":"1","chatUrl":false}]
const mentors = [{"id":"0","firstName":"Daniel","about":false},{"id":"1","firstName":"Mark","aboutl":false},{"id":"3","firstName":"Eric","about":false},{"id":"6","firstName":"John","about":false}]

mentors.sort((a, b) => a.firstName.localeCompare(b.firstName))
const order = mentors.reduce((r, { id }, i) => (r[id] = i, r), {})
groups.sort((a, b) => order[a.mentorId] - order[b.mentorId])
console.log(groups)

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

Comments

1
var group= [
     
     {id: "17", name: "C1", courseId: "7", mentorId: "3", chatUrl: false, students: {0: 15, 1: 16, 2: 18}},
     {id: "16", name: "P1", courseId: "6", mentorId: "1", chatUrl: false, students: {0: 1, 1: 10, 2: 11}},
     {id: "22", name: "P2", courseId: "6", mentorId: "1", chatUrl: false, students: {0: 12, 1: 13, 2: 9}},
     {id: "23", name: "C2", courseId: "7", mentorId: "3", chatUrl: false, students: {0: 17, 1: 19, 2: 20}},
     {id: "24", name: "DEV", courseId: "10", mentorId: "1", chatUrl: false}] 

var mentor=[
    {id: "0", firstName: "Daniel",  about: false},
    {id: "1", firstName: "Mark",  aboutl:false},
    {id: "3", firstName: "Eric", about: false},
    {id: "6", firstName: "John", about: false} 
]

group = group.sort( (a,b) => {
    for(i=0;i<mentor.length;i++){
        if (a.mentorId===mentor[i].id){a.mentorName=mentor[i].firstName}
        if (b.mentorId===mentor[i].id){b.mentorName=mentor[i].firstName}
        
    }
    if(a.mentorName<b.mentorName){return -1} 
    else if(a.mentorName==b.mentorName){return 0} 
    else{return 1}
});
console.log(group);

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.