Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- You are given a list of marks over 5 subjects scored by n students. You need to sort the student by following rules.
- The student whose sum of marks will be highest must come at the top of the leaderboard.
- Students whose total sum of marks are the same will be sorted alphabetically ascending by name.
- In all other cases, student with lesser id will come before in the leaderboard
- Input format
- First line will contain a single integer n number of students.
- Next n lines will contain space separated unique integer Id, string S name of the student followed by 5 integers marks scored out of 100.
- Output format
- Print space separated list of sorted ids of the student in a single line
- Sample Input 1
- 3
- 1 Alan 45 56 21 32 74
- 2 Fabien 95 94 93 92 91
- 3 Gamora 85 65 54 65 76
- Sample Output 1
- 2 3 1
- Explanation 1
- Fabien has the most marks obtained and then Gamora and followed by Alan
- Constraints
- 1<=n<=50000
- 1<=|S|<=10
- Name of the Student will be in alphabetically lowercase characters.
- 1<=Marks[i]<=100
- 1<=Student id<=10^7
- Student id will be unique for each student
- */
- /**
- * @param {number} n
- * @return {number[]}
- */
- function marksSort(n, students) {
- students.map(student=>{
- let totalMarks=0;
- student.slice(2).map(mark=>totalMarks=totalMarks+mark)
- student.push(totalMarks);
- })
- // console.log(students)
- // --ive -> ascending order
- students.sort((student1,student2)=>{
- const totalMarksComparision=student2[7]-student1[7];
- if(totalMarksComparision==0){
- const nameComparision=student1[1].localeCompare(student2[1]);// - , + , 0
- if(nameComparision ==0){
- return student1[0]-student2[0];
- }
- else
- return nameComparision;
- }
- else
- return totalMarksComparision;
- })
- let sortedId= [...students.map(student=>student.slice(0,1))];
- return sortedId;
- }
- /*
- That's a great question. The reason for the difference in ordering between `student1[1].localeCompare(student2[1])` and `student2[7] - student1[7]` is related to the desired sorting order for each comparison.
- 1. **Name Comparison (`student1[1].localeCompare(student2[1])`):** In this case, we use `student1[1].localeCompare(student2[1])` to compare names alphabetically. The standard behavior of `localeCompare` is to return a negative value when the calling string (in this case, `student1[1]`) comes before the argument string (`student2[1]`) in alphabetical order. So, `student1[1].localeCompare(student2[1])` ensures that names are sorted in ascending order (A to Z).
- 2. **Total Marks Comparison (`student2[7] - student1[7]`):** In this case, we use `student2[7] - student1[7]` to compare total marks. When we subtract `student1[7]` from `student2[7]`, the result is negative if `student1[7]` is less than `student2[7]`. Therefore, this comparison ensures that total marks are sorted in descending order (highest marks first).
- The key difference is the desired sorting order for each comparison:
- - For names, we want ascending alphabetical order, so we use `student1[1].localeCompare(student2[1])`.
- - For total marks, we want descending order (highest marks first), so we use `student2[7] - student1[7]`.
- These differences reflect the specific sorting criteria mentioned in your original question.
- */
- function main() {
- let n = parseInt(readLine());
- let students = [];
- for (let i = 0; i < n; i++) {
- let student = readLine().split(" ");
- for (let j in student) {
- if (j != 1) student[j] = parseInt(student[j]);
- }
- students.push(student);
- }
- let sortedId = marksSort(n, students);
- print(sortedId.join(" "));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement