I need some guidance on how to apply sorting to a table in which I display scores to respondent and I want to show them in descending order. Full detail: Respondent completes survey about career skills/interests, survey scores responses, and at survey end I have a table in a “Text/Graphic” question type that displays each category name in column A and corresponding score (piped in) in column B. I need that table to sort before/as it displays to respondent so that rows are listed in descending order of scores for categories. I found some HTML coding online for how to sort table, but all examples presume you “know” the value and it is hard coded in (literally,. as a value “4” as opposed to a variable). So, I’m at a loss of how to translate that to a table using piped in values that of course change respondent to respondent. Here is table. TIA for guidance and recommendations.
Accounting Roles
${gr://SC_9QZAkIXimOoFX3U/Score}
Administrative Support
${gr://SC_9Sk1b7bsvGViI0C/Score}
HR Roles
${gr://SC_8e1FPLkGOhD9Xka/Score}
IT Roles
${gr://SC_1SnnTDgQYMdp50W/Score}
Medical Assistant
${gr://SC_9XpyOjAm22Vh4oK/Score}
OR Technician
${gr://SC_aY0vI2hxiXQJB5k/Score}
Patient Care Technician
${gr://SC_1AEhAHXq5R0q1z8/Score}
Pharmacy Technician
${gr://SC_cMFdcumXg46uWJo/Score}
Radiology Technician
${gr://SC_4GFVCFdGtUmydeK/Score}
RN
${gr://SC_1MluKM4Uup0pFBA/Score}
Sous Chef
${gr://SC_d6DfNj8MRgAvmD4/Score}
Best answer by Nam Nguyen
I presume your recommending putting it in the Question javascript in the onload section? I have done so and table remains sorted as I have it programmed into table - it does not sort by value of score in column b.
@SeanMeegan How are you setting this up because it’s working on my size. This is how I set it up: 1. A HTML table in text/graphic question with scoring as value
Custom JavaScript in that text/graphic to sort the table as it load
Qualtrics.SurveyEngine.addOnload(function() { var table = document.querySelector('table'); var rows = Array.from(table.querySelectorAll('tr'));
rows.sort(function(a, b) { var scoreA = parseFloat(a.cells[1].textContent); var scoreB = parseFloat(b.cells[1].textContent); return scoreB - scoreA; // Sort in descending order });
I presume your recommending putting it in the Question javascript in the onload section? I have done so and table remains sorted as I have it programmed into table - it does not sort by value of score in column b.
I presume your recommending putting it in the Question javascript in the onload section? I have done so and table remains sorted as I have it programmed into table - it does not sort by value of score in column b.
@SeanMeegan How are you setting this up because it’s working on my size. This is how I set it up: 1. A HTML table in text/graphic question with scoring as value
Custom JavaScript in that text/graphic to sort the table as it load
Qualtrics.SurveyEngine.addOnload(function() { var table = document.querySelector('table'); var rows = Array.from(table.querySelectorAll('tr'));
rows.sort(function(a, b) { var scoreA = parseFloat(a.cells[1].textContent); var scoreB = parseFloat(b.cells[1].textContent); return scoreB - scoreA; // Sort in descending order });