Home > Net >  How To Sort Ranking By Score, Gender, & Name using either Javascript or JQuery
How To Sort Ranking By Score, Gender, & Name using either Javascript or JQuery

Time:01-16

I have three arrays like below (I have large data of like 25000, so you may also please advise if this is a good approach for that amount of data):

var arrayScores =    [10, 21, 2, 35, 19, 85, 96, 70,  85,  19, 100, 11, 35, 70, 98, 81, 92];
var arrayGenders =   ['M', 'M', 'F', 'F', 'M', 'M', 'M', 'F',  'F',  'M',  'M', 'F', 'F', 'M', 'M', 'F', 'F'];
var arrayNames =     ['John', 'Fred', 'Jacque', 'Andrea', 'Andrew', 'Jacob', 'Dylan', 'Diana',  'Rahel',  'Raphael',  'Bob', 'Mariam', 'Fatma', 'Ahmed', 'Barack', 'Joyce', 'Taina'];

So I want to sort these arrays ranks by scores (in descending order), then by gender (in ascending order), and lastly by name in (ascending order) as shown below :

1.Bob-100, 2.Barack-98, 3.Dylan-96, 4.Taina-92, 5.Rahel-85, 6.Jacob-85, 7.Joyce-81, 8.Diana-70, 9.Ahmed-70, 10.Andrea-35, 11.Fatma-35, 12.Fred-21, 13.Andrew-19, 14.Raphael-19, 15.Mariam-11, 16.John-10, 17.Jacque-2

Here is my code, but it doesn't provide what I expect as I don't know how to sort it (I'm still a beginner learning JavaScript), Please help!

var arrayScores =    [10, 21, 2, 35, 19, 85, 96, 70,  85,  19, 100, 11, 35, 70, 98, 81, 92];
var arrayGenders =   ['M', 'M', 'F', 'F', 'M', 'M', 'M', 'F',  'F',  'M',  'M', 'F', 'F', 'M', 'M', 'F', 'F'];
var arrayNames =     ['John', 'Fred', 'Jacque', 'Andrea', 'Andrew', 'Jacob', 'Dylan', 'Diana',  'Rahel',  'Raphael',  'Bob', 'Mariam', 'Fatma', 'Ahmed', 'Barack', 'Joyce', 'Taina'];
var rank = 1;
arrayScores.forEach((score, index) => {
  const gender = arrayGenders[index], name = arrayNames[index];
  console.log("Name: " name " Gender: " gender  " Score: " score  " Rank: " rank);
rank  ;
});

CodePudding user response:

You could build objects and sort in a single run.

This approach takes an object with same names as the final propertiers and creates from the entries an array of objects.

The sorting takes place with the wanted properties. For direct values, like gender, you could take an object for the wanted order. This allows to add more genders and orders.

const
    score = [10, 21, 2, 35, 19, 85, 96, 70, 85, 19, 100, 11, 35, 70, 98, 81, 92],
    gender = ['M', 'M', 'F', 'F', 'M', 'M', 'M', 'F', 'F', 'M', 'M', 'F', 'F', 'M', 'M', 'F', 'F'],
    name = ['John', 'Fred', 'Jacque', 'Andrea', 'Andrew', 'Jacob', 'Dylan', 'Diana', 'Rahel', 'Raphael', 'Bob', 'Mariam', 'Fatma', 'Ahmed', 'Barack', 'Joyce', 'Taina'],
    object = { score, gender, name },
    genders = { F: 1, M: 2 },
    data = Object
        .entries(object)
        .reduce((r, [k, values]) => values.map((v, i) => ({ ...r[i], [k]: v })), [])
        .sort((a, b) =>
            b.score - a.score ||
            genders[a.gender] - genders[b.gender]||
            a.name.localeCompare(b.name)
        );

console.log(data);
.as-console-wrapper { max-height: 100% !important; top: 0; }

  • Related