Welcome to the LimeSurvey Community Forum

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

Sort answers alphabetically (alphasort)

  • jimdavies
  • jimdavies's Avatar Topic Author
  • Offline
  • Senior Member
  • Senior Member
More
5 years 8 months ago #172137 by jimdavies
Sort answers alphabetically (alphasort) was created 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.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
5 years 8 months ago #172140 by tpartner
Replied by tpartner on topic Sort answers alphabetically (alphasort)
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.
  • jimdavies
  • jimdavies's Avatar Topic Author
  • Offline
  • Senior Member
  • Senior Member
More
5 years 8 months ago #172141 by jimdavies
Replied by jimdavies on topic Sort answers alphabetically (alphasort)
New answer options will be added as new suppliers come on board.
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
5 years 8 months ago #172142 by tpartner
Replied by tpartner on topic Sort answers alphabetically (alphasort)
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.

Lime-years ahead

Online-surveys for every purse and purpose