Welcome to the LimeSurvey Community Forum

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

Sort Multiple Choice subquestions

  • blocka
  • blocka's Avatar Topic Author
  • Offline
  • Platinum Member
  • Platinum Member
More
6 years 8 months ago #156090 by blocka
Sort Multiple Choice subquestions was created 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.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
6 years 8 months ago - 6 years 8 months ago #156092 by tpartner
Replied by tpartner on topic Sort Multiple Choice subquestions
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: 6 years 8 months ago by tpartner.
The following user(s) said Thank You: blocka
The topic has been locked.
  • blocka
  • blocka's Avatar Topic Author
  • Offline
  • Platinum Member
  • Platinum Member
More
6 years 8 months ago #156093 by blocka
Replied by blocka on topic Sort Multiple Choice subquestions
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.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
6 years 8 months ago - 6 years 8 months ago #156109 by tpartner
Replied by tpartner on topic Sort Multiple Choice subquestions
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: 6 years 8 months ago by tpartner.
The topic has been locked.
  • Mazi
  • Mazi's Avatar
  • Offline
  • Official LimeSurvey Partner
  • Official LimeSurvey Partner
More
3 years 6 months ago #204896 by Mazi
Replied by Mazi on topic Sort Multiple Choice subquestions
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
Need Help? We offer professional Limesurvey support: survey-consulting.com
Contact: marcel.minke(at)survey-consulting.com
The topic has been locked.

Lime-years ahead

Online-surveys for every purse and purpose