Sort categories based on score | XM Community
Question

Sort categories based on score

  • 12 January 2021
  • 5 replies
  • 21 views

Hi -
Hoping someone can help me out with this.
I have a survey/personality test which yields scores in 4 categories. Is there a way to show a message that displays the category names in descending order at the of the survey.
For example:
A = 4
B = 10
C = 2
D = 8
Display: B D A C
Thanks,
Meiling


5 replies

Userlevel 7
Badge +21

If you can source the score for each category from an embedded data, then you can use the code below. It basically pastes the scores in the choices of a multiple choice question. You can look up string manipulations online if you want to paste in a descriptive type question.
Your first line would look something like this :

ans = { A: "${e://Field/score_a}", B: "${e://Field/score_b}" ....... }

Qualtrics.SurveyEngine.addOnReady(function(){
ans = {A:"4", B:"10",C:"2",D:"8"}
text_entry = [];
sorted_ans = [];
for (var score in ans) {
sorted_ans.push([score, ans[score]]);
}
sorted_ans.sort(function(a, b) {
return a[1] - b[1];
});
sorted_ans.forEach(ent => {
text_entry.push("The score for "+ent[0]+" is "+ent[1])});

text_entry.reverse();
for(i=0;i this.getChoiceContainer().querySelectorAll("span label").innerText = text_entry[i];
}
});

Hi Ahmed - thanks for your help. I tried putting it in to a descriptive question at the end (the categories are F, I, S, H and I have the embedded data that shows the scores). However, when I preview the survey, it just displays the coding language...where do I go from here (I don't have any coding background).

Here are your F.I.S.H. scores:

Flexible: ${gr://SC_bCOtuGSckQp3Aqx/Score}
Informed: ${gr://SC_erIpxxnGTGkQgvj/Score}
Structured: ${gr://SC_5oqd1krTjVRweX3/Score}
Harmonious: ${gr://SC_0NhlpiPMt1nvR2d/Score}
 

ans = { F: "${gr://SC_bCOtuGSckQp3Aqx/Score}", I: "${gr://SC_erIpxxnGTGkQgvj/Score}", S: "${gr://SC_5oqd1krTjVRweX3/Score}", H: "${gr://SC_0NhlpiPMt1nvR2d/Score}"}

Qualtrics.SurveyEngine.addOnReady(function(){ ans = {F:"4",I:"10",S:"2",H:"8"} text_entry = []; sorted_ans = []; for (var score in ans) { sorted_ans.push([score, ans[score]]); } sorted_ans.sort(function(a, b) { return a[1] - b[1]; }); sorted_ans.forEach(ent => { text_entry.push("The score for "+ent[0]+" is "+ent[1])}); text_entry.reverse();for(i=0;i
}

});

Userlevel 7
Badge +21

Either change the descriptive text question to a multiple choice with 4 choices or change the last line to the following:

this.getQuestionContainer().innerHTML += text_entry[i] + "
";

Ok - now it displays the scores but how can the code be hidden and just have the user the categories sorted by descending score displayed to the user - so in this case:
IFHS


image.png

Userlevel 7
Badge +21

Please update the question or create a new question with your EXACT detailed requirements and ALL the relevant details about your current setup.

Leave a Reply