Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

198
Views
javascript nested array sort, based on variable-length inner rankings array

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.

about 4 years ago · Santiago Trujillo
1 answers
Answer question

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.

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!