Sort answers alphabetically (alphasort)

More
1 week 2 days ago #172137 by jimdavies
jimdavies created the topic: Sort answers alphabetically (alphasort)
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

Please Log in or Create an account to join the conversation.

More
1 week 1 day ago #172140 by tpartner
tpartner replied the 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.

Please Log in or Create an account to join the conversation.

More
1 week 1 day ago #172141 by jimdavies
jimdavies replied the topic: Sort answers alphabetically (alphasort)
New answer options will be added as new suppliers come on board.

Please Log in or Create an account to join the conversation.

More
1 week 1 day ago #172142 by tpartner
tpartner replied the topic: Sort answers alphabetically (alphasort)
Add something like this to the source of the question:

<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.

Please Log in or Create an account to join the conversation.

Start now!

Just create your account and start using Limesurvey today.

Register now
Join our Newsletter!