Welcome to the LimeSurvey Community Forum

Ask the community, share ideas, and connect with other LimeSurvey users!

Sort Multiple Choice subquestions

More
9 years 3 months ago #156090 by blocka
For radio / dropdown question types, LS gives us the option to sort the subquestions alphabetically.

For multiple choice, this option doesn't exist. I wonder if anyone has a workaround? I have a bilingual survey with about 50 subquestions for one multiple choice survey -- while I can sort one language by entering them in correct alpha order, I can't do that the secondary language because of course, the answer codes have to match.
The topic has been locked.
More
9 years 3 months ago - 9 years 3 months ago #156092 by tpartner
Insert a script like this into the question text:

Code:
<script type="text/javascript" charset="utf-8"> $(document).ready(function() { // Identify this question var qID = {QID}; var thisQuestion = $('#question'+qID); // Build an array of the sub-question texts var sqTexts = []; $('.label-text', thisQuestion).each(function(i) { sqTexts.push($.trim($(this).text())); }); // Sort the array sqTexts.sort(); // Insert the sub-questions in sorted order $(sqTexts).each(function(i, value) { $('.label-text', thisQuestion).filter(function(e) { return $.trim($(this).text()) === value; }).closest('.answer-item').parent().appendTo($('.multiple-choice-container', thisQuestion)); }); }); </script>

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Last edit: 9 years 3 months ago by tpartner.
The following user(s) said Thank You: blocka
The topic has been locked.
More
9 years 3 months ago #156093 by blocka
Thanks, that's sweet!

I wonder -- any way that the script can respect the 2 column layout for the question (this was something I set up -- the two columns to make the list a bit less intimidating)...

Maybe there is no solution for that, and that's okay, because the sort order is more important than two columns!
The topic has been locked.
More
9 years 3 months ago - 9 years 3 months ago #156109 by tpartner
This script should handle columns (in version 2.65.x):

Code:
<script type="text/javascript" charset="utf-8"> $(document).ready(function() { // Identify this question var qID = {QID}; var thisQuestion = $('#question'+qID); // Define column and items counts var itemsCount = $('.answer-item', thisQuestion).length; var columnsCount = $('.multiple-choice-container div.row', thisQuestion).length; if(columnsCount > 1) { var maxPerColumn = $('.multiple-choice-container div.row:eq(0) .answer-item', thisQuestion).length; } // Build an array of the sub-question texts var sqTexts = []; $('.label-text', thisQuestion).each(function(i) { sqTexts.push($.trim($(this).text())); }); // Sort the array sqTexts.sort(); // Insert the sub-questions in sorted order var currentCount = 1; var columnNumber = 0; $(sqTexts).each(function(i, value) { if(columnsCount < 1) { $('.label-text', thisQuestion).filter(function(e) { return $.trim($(this).text()) === value; }).closest('.answer-item').parent().appendTo($('.multiple-choice-container', thisQuestion)); } else { $('.label-text', thisQuestion).filter(function(e) { return $.trim($(this).text()) === value; }).closest('.answer-item').parent().appendTo($('.multiple-choice-container div.row:eq('+columnNumber+')', thisQuestion)); currentCount++; if(currentCount > maxPerColumn) { currentCount = 1; columnNumber++; } } }); }); </script>

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Last edit: 9 years 3 months ago by tpartner.
The topic has been locked.
More
6 years 1 month ago #204896 by Mazi
Great solution, Tony!

Do you also have a JS snippet to sort Array questions accoridng to the subquestion titles (first column) alphabetically?

Best regards/Beste Grüße,
Dr. Marcel Minke
survey-consulting.com
offlinesurveys.com
Feel free to contact me by email for professional LimeSurvey support!
The topic has been locked.
Moderators: tpartner, holch

Lime-years ahead

Online-surveys for every purse and purpose