Choice Sort Function | XM Community
Skip to main content

Hello,
I have a Pick, Group, and Rank question and I am looking to return a sorted list using JavaScript. My code is as follows:
Qualtrics.SurveyEngine.addOnload(function()
{   
/*Place your JavaScript here to run when the page loads*/   
var choice = i
"${q://QID1/ChoiceNumericEntryValue/Rank/1}","${q://QID1/ChoiceDescription/1}"],
]"${q://QID1/ChoiceNumericEntryValue/Rank/2}","${q://QID1/ChoiceDescription/2}"],
]"${q://QID1/ChoiceNumericEntryValue/Rank/3}","${q://QID1/ChoiceDescription/3}"],
]"${q://QID1/ChoiceNumericEntryValue/Rank/4}","${q://QID1/ChoiceDescription/4}"]
];         

choice.sort(function(a, b){return a - b});

Qualtrics.SurveyEngine.setEmbeddedData('first',choice(0]i1]);   
Qualtrics.SurveyEngine.setEmbeddedData('second',choicet1]'1]);   
Qualtrics.SurveyEngine.setEmbeddedData('third',choicee2]a1]);   
Qualtrics.SurveyEngine.setEmbeddedData('fourth',choiced3]d1]);
});
For some reason, even after doing choice.sort, I just get a static result of
ChoiceDescription/1
ChoiceDescription/2
ChoiceDescription/3
ChoiceDescription/4
in that order without any sorting, and regardless of what was put into the block. Can someone help me to figure out why it's not sorting my rankings? Thank you in advance for your help.

tobyhkim
Your code is correct but with a minor change you can achieve the desired result.
Just include the code on the question which is on next page and not on the pick group rank question. Also remove:
"(function(a, b){return a - b});"
And keep your code in the manner shown below:
Qualtrics.SurveyEngine.addOnload(function()
{   
/*Place your JavaScript here to run when the page loads*/   
var choice = i
"${q://QID1/ChoiceNumericEntryValue/Rank/1}","${q://QID1/ChoiceDescription/1}"],
]"${q://QID1/ChoiceNumericEntryValue/Rank/2}","${q://QID1/ChoiceDescription/2}"],
]"${q://QID1/ChoiceNumericEntryValue/Rank/3}","${q://QID1/ChoiceDescription/3}"],
]"${q://QID1/ChoiceNumericEntryValue/Rank/4}","${q://QID1/ChoiceDescription/4}"]
];         

choice.sort();

Qualtrics.SurveyEngine.setEmbeddedData('first',choice(0]i1]);   
Qualtrics.SurveyEngine.setEmbeddedData('second',choicet1]'1]);   
Qualtrics.SurveyEngine.setEmbeddedData('third',choicee2]a1]);   
Qualtrics.SurveyEngine.setEmbeddedData('fourth',choiced3]d1]);
});
Your result would be the descriptions of choice which were kept first second third and fourth in respective embedded data.
Hope it helps!


You have to sort an array of objects not an array of arrays.
var choice = [
{rank:"${q://QID1/ChoiceNumericEntryValue/Rank/1}",desc:"${q://QID1/ChoiceDescription/1}"},
{rank:"${q://QID1/ChoiceNumericEntryValue/Rank/2}",desc:"${q://QID1/ChoiceDescription/2}"},
...etc...
];       
Then you can sort by a field or fields in the objects.
BTW, if you are interested, I have a function, captureRanks, that you can add to the PGR question that captures ranks. No need for piping or additional questions with JS. It supports multiple PGR groups too.


https://community.qualtrics.com/XMcommunity/discussion/comment/51734#Comment_51734Deepak Thank you for your response! Unfortunately, this change also does not achieve the desired result. I get the descriptions in the same order, regardless of how I rank the four choices...


https://community.qualtrics.com/XMcommunity/discussion/comment/51735#Comment_51735TomG Hi Tom, thank you for your response! Unfortunately, I've tried that too and it still returns the 4 choices that is not sorted by the ranking... I will look at the captureRanks function and play around with it too!


https://community.qualtrics.com/XMcommunity/discussion/comment/51736#Comment_51736tobyhkim
I did it with 2 groups and I received the result in sorted manner including image below of console.
image.pngNot sure, what is not working here.


Deepak
I'm not sure how I can get to the console for Qualtrics, but for clarity, I have the question set up like this:
Screen Shot 2022-11-03 at 3.30.45 PM.pngEven if I answer the following:
Screen Shot 2022-11-03 at 3.35.28 PM.pngI get the following result after putting the javascript into a blank question on a new block on the next page:
Screen Shot 2022-11-03 at 3.35.53 PM.pngSorry for being difficult!


tobyhkim
The issue here is you have 12 items and you have kept only 4 in your code.
Your code should include all the 12 items only then you will be able to sort among them and show.
So it would be like: (I have included the console line as well)

Qualtrics.SurveyEngine.addOnload(function()
{   
/*Place your JavaScript here to run when the page loads*/   
var choice = i
"${q://QID1/ChoiceNumericEntryValue/Rank/1}","${q://QID1/ChoiceDescription/1}"],
]"${q://QID1/ChoiceNumericEntryValue/Rank/2}","${q://QID1/ChoiceDescription/2}"],
]"${q://QID1/ChoiceNumericEntryValue/Rank/3}","${q://QID1/ChoiceDescription/3}"],
]"${q://QID1/ChoiceNumericEntryValue/Rank/4}","${q://QID1/ChoiceDescription/4}"],
]"${q://QID1/ChoiceNumericEntryValue/Rank/5}","${q://QID1/ChoiceDescription/5}"],
]"${q://QID1/ChoiceNumericEntryValue/Rank/6}","${q://QID1/ChoiceDescription/6}"],
]"${q://QID1/ChoiceNumericEntryValue/Rank/7}","${q://QID1/ChoiceDescription/7}"],
]"${q://QID1/ChoiceNumericEntryValue/Rank/8}","${q://QID1/ChoiceDescription/8}"],
]"${q://QID1/ChoiceNumericEntryValue/Rank/9}","${q://QID1/ChoiceDescription/9}"],
]"${q://QID1/ChoiceNumericEntryValue/Rank/10}","${q://QID1/ChoiceDescription/10}"],
]"${q://QID1/ChoiceNumericEntryValue/Rank/11}","${q://QID1/ChoiceDescription/11}"],
]"${q://QID1/ChoiceNumericEntryValue/Rank/12}","${q://QID1/ChoiceDescription/12}"]
];         
console.log(choice)
choice.sort();

Qualtrics.SurveyEngine.setEmbeddedData('first',choice(0]i1]);   
Qualtrics.SurveyEngine.setEmbeddedData('second',choicet1]'1]);   
Qualtrics.SurveyEngine.setEmbeddedData('third',choicee2]a1]);   
Qualtrics.SurveyEngine.setEmbeddedData('fourth',choiced3]d1]);
});


Now since you have two groups the first, second, third, fourth would be the first 4 in array which is sorted out and not first 4 from first group or second group. You will have to manage that separately.
You can pick the group Id from the below code and append it if needed
jQuery('#QID1').find('ul')u1].id;
jQuery('#QID1').find('ul')u2].id;
Hope it helps!


Deepak thank you so, so much! I think I was able to work to troubleshoot this.
For some reason, the code only works if I put all of the choices in one of the groups. Is there a way to make it so that the code only runs and works for the selections in group ID 1?
I'm not sure where to append the jQuery since it's the first time I would be using it. Thank you again, so much for your help!


Deepak just wanted to follow-up on this question because I'm so close to getting the perfect desired result!


Leave a Reply