Outer array should be sorted based on the combination of rankings of each student's variable-length inner "tests" array, ascending. Here's a simplified version of the raw data.
[
{
"studentId": "A",
"tests": [
{
"testId": 100,
"rank": 2
},
{
"testId": 101,
"rank": 1
},
{
"testId": 102,
"rank": 1
}
]
},
{
"studentId": "G",
"tests": [
{
"testId": 103,
"rank": 3
},
{
"testId": 104,
"rank": 6
},
{
"testId": 105,
"rank": 3
},
{
"testId": 106,
"rank": 4
},
{
"testId": 107,
"rank": 3
}
]
}
]
For brevity, here is an illustration commented to capture the rules for sorting and tie-breaking. Imagine each of the below represents the pre-sorted sequence of each student's individual test ranks. (Note that testIds don't matter, other than to preserve them. We're not concerned with comparing individual tests, only each student's overall combination of ranks):
StudentId : [ascending sequence of test ranks]
A : [ 1, 1, 2] // A beats B due to 2nd element
B : [ 1, 2, 2] // B beats C due to 1st element
C : [ 2, 3] // Let C beat D due to shorter length!
D : [ 2, 3, 3] // D beats E due to 1st element
E : [ 3, 3, 3, 4, 5, 7] // E beats F and G due to 5th element
F : [ 3, 3, 3, 4, 6] // F and G are tied. Let F beat G due to studentId! (alphanumeric ascending)
G : [ 3, 3, 3, 4, 6]
None of the raw data is sorted. Finished output to include sorted outer array, with each student's test array sorted by rank. Please let me know if anything's unclear, and thank you.
So, basically, the array needs to be sorted based on sum of the test ranks in ascending order, yes?
function sumTestRanks(students) {
for(let student of students) {
sumStudentTestRanks(student);
}
}
function sumStudentTestRanks(student) {
let testsRankTotal = 0;
const tests = student.tests;
for(let test of tests) {
testsRankTotal += test.rank;
}
student.testsRankTotal = testsRankTotal;
}
function studentsSorter(studentA, studentB) {
if(studentA.testsRankTotal < studentB.testsRankTotal) {
return -1;
}
if(studentA.testsRankTotal > studentB.testsRankTotal) {
return 1;
}
return studentA.studentId.localeCompare(studentB.studentId);
}
const students = [...];
sumTestRanks(students)
console.log(students.sort(studentsSorter));
Update:
In that case, it goes like this:
function studentsSorter(studentA, studentB) {
const studentATests = studentA.tests;
const studentBTests = studentB.tests;
const studentATestsLength = studentATests.length;
const studentBTestsLength = studentBTests.length;
let scanLength = studentATestsLength;
if(scanLength > studentBTestsLength) {
scanLength = studentBTestsLength;
}
for(let index = 0; index < scanLength; index++) {
const studentATest = studentATests[index];
const studentBTest = studentBTests[index];
if(studentATest.rank === studentBTest.rank) {
continue;
}
if(studentATest.rank < studentBTest.rank) {
return -1;
}
else if(studentATest.rank > studentBTest.rank) {
return 1;
}
}
if(studentATestsLength === studentBTestsLength) {
return studentA.studentId.localeCompare(studentB.studentId);
}
else if(studentATestsLength < studentBTestsLength) {
return -1;
}
else {
return 1;
}
}
I referred this answer to understand how comparator works in JavaScript.