Welcome to the LimeSurvey Community Forum

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

Sort answers alphabetically (alphasort)

More
7 years 1 month ago #172137 by jimdavies
I can't find the option to sort answers in a drop down to be in alphabetically order.

I'm on Version 2.50+ Build 160810
The topic has been locked.
More
7 years 1 month ago #172140 by tpartner
There is no built-in facility to do that. Why not sort the answer options before loading them?

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.
More
7 years 1 month ago #172141 by jimdavies
New answer options will be added as new suppliers come on board.
The topic has been locked.
More
7 years 1 month ago #172142 by tpartner
Add something like this to the source of the question:

Code:
<script type="text/javascript" charset="utf-8">
 
  $(document).on('ready pjax:scriptcomplete',function(){
 
    // Identify this question
    var thisQuestion = $('#question{QID}');
 
    // Create an array of dropdown option text values
    var optionTexts = [];
    $('select.form-control:eq(0) option', thisQuestion).each(function(i) {
      if($(this).val() != '') {
        optionTexts.push($.trim($(this).text()));
      }
    });
 
    // Sort the array
    optionTexts.sort(function (a, b) {
      return a.toLowerCase().localeCompare(b.toLowerCase());
    });
 
    //Loop through the option texts and insert the corresponding drop-down options
    $.each(optionTexts, function(key, val) {
 
      $('select.form-control:eq(0) option', thisQuestion).filter(function() {
        return $.trim($(this).text()).toLowerCase() == val.toLowerCase();
      }).appendTo($('select.form-control:eq(0)', thisQuestion));
    });
    });
</script>

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.
Moderators: tpartnerholch

Lime-years ahead

Online-surveys for every purse and purpose