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

155
Views
JavaScript sort multiple array

Suppose I have this data

Name Mark
John 76
Jack 55
Dani 90

and for the grade

Marks Grade
100-80 A
79 - 60 B
59 - 40 C

suppose i declare the script as

 let data = [
  [John, 76],
  [Jack, 55],
  [Dani, 90]
];

The program should assign the grade with the corresponding mark, how do I sort the grade since we know we cant change the index for mark as usual because each mark assign to different student? The output should display all data in descending order as

Name Mark Grade
Dani 90 A
John 76 B
Jack 55 C
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

I would break it up into different functions so that you can handle each task separately. Then you can combine them to produce your desired result, like this:

const grades = [
  ['A', 80],
  ['B', 60],
  ['C', 40],
];

function getGrade (mark) {
  for (const [grade, minMark] of grades) {
    if (mark < minMark) continue;
    return grade;
  }
  return 'F'; // use minimum grade as default if mark is too low
}

function mapToObject ([name, mark]) {
  return {grade: getGrade(mark), name, mark};
}

function sortByHighestMark (a, b) {
  return b.mark - a.mark;
}

const data = [
  ['John', 76],
  ['Jack', 55],
  ['Dani', 90]
];

const result = data.map(mapToObject).sort(sortByHighestMark);
console.log(result);

// and data is unmodified:
console.log(data);

about 4 years ago · Juan Pablo Isaza Report

0

If you mean that you expect output like:

Dani - A
John - B
Jack - C

Then I would do something like:

A method:

getGrade(points => {
  if(points >= 80 && points <= 100) return 'A'
  if(points >= 60 && points < 80) return 'B'
  return 'C'
})

Then have data as:

    const data = [
                 {name: 'John', value: 76},
                 {name: 'Jack', value: 55},
                 {name: 'Dani', value: 90},
                 ]

Then just do:

data.sort((student1, student2) => {
 if(getGrade(student1.value) === student2.value) return 0
 return (getGrade(student1.value) > getGrade(student2.value))? 1 : -1
})

I hope I understood you correct?

about 4 years ago · Juan Pablo Isaza 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!