-1

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.

1 Answers1

0

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:

  1. Determine minimum comparable size
  2. Do comparison within that length
  3. If it is a tie within the comparable size
    • Compare by id, if both tests sizes are same.
    • Lastly, compare by length
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.

  • Thanks for replying. Not quite sum of the test ranks. Rather for each student, line up their test ranks in ascending order, then compare each's best test rank first. If there's a tie, then compare their 2nd best test rank, etc. Until there's either a clear winner, or if one of the two students that's being compared runs out of tests, they win that comparison. Or if there's a complete tie across an equal number of tests, then the fallback is to choose based on studentId. – thoroughlyConfused Apr 23 '22 at 06:08
  • @thoroughlyConfused, check updated answer. – Tamil Vendhan Kanagarasu Apr 23 '22 at 09:52
  • Hi Tamil. We're getting closer! A few things: 1) Each student's test ranks need to be sorted from best to worst, _before_ doing the rank by rank comparison. 2) The shorter length comparison is used to break a tie between two students, for when their ranks are identical up until that point. 3) studentId comparison should come last, only for when two students have identical ranks _and_ lengths. Thanks again – thoroughlyConfused Apr 24 '22 at 00:03
  • P.s. I tried to capture these rules in the "illustration", 2nd code block of original post. – thoroughlyConfused Apr 24 '22 at 00:14