Display Answer Choices in Alpha Order | XM Community
Skip to main content

Hi! Does anyone know of a way to display answer choices in alphabetical order in a multiple choice question? For example, we have a question where the respondent selects their instructor name from a multiple choice list. There are 212 instructors. When the survey was built, we were able to paste the instructor names into the answer choices alphabetically from a sorted MS Excel spreadsheet.
We were just asked to add 25 instructors to this question. Since we have already collected data in the survey, the only option is to add the names to the bottom of the answer list and manually click an arrow to move them up until they display in alpha order. This is a pain to do...
Any help would be greatly appreciated!

Add the below code in addOnload it will sort your list in Alphabetical order,

var mylist = jQuery('.ChoiceStructure');
var listitems = mylist.children('li').get();
listitems.sort(function(a, b) {
  return jQuery(a).text().toUpperCase().localeCompare(jQuery(b).text().toUpperCase());
})
jQuery.each(listitems, function(idx, itm) { mylist.append(itm); });


https://www.qualtrics.com/community/discussion/comment/26494#Comment_26494SurajK
Thank you so much! I pasted it and tested it and the answer choices didn't appear in alpha order. Did I do it correctly (I haven't used JavaScript in 15+ years)?
image.png


Can you try in addOnReady function, it might work for. The above code works based on your class of yout list, for me it is ChoiceStructure, please check your class or id and update it.


https://www.qualtrics.com/community/discussion/comment/26500#Comment_26500How do you find the class or ID of the list?


try this instead,

var mylist = jQuery('ul');
var listitems = mylist.children('li').get();
listitems.sort(function(a, b) {
  return jQuery(a).text().toUpperCase().localeCompare(jQuery(b).text().toUpperCase());
})
jQuery.each(listitems, function(idx, itm) { mylist.append(itm); });


I pasted that into both onLoad and onReady and it didn't work :(


MeganS - I'm not sure how you are following the code, the list will get alphabetize at the run time, once you add the code in JS, take a preview link, you should be able to see the answer list sorted alphabetically.


https://www.qualtrics.com/community/discussion/comment/26534#Comment_26534Hi! That is what I'm doing but unfortunately it is not working :(


MeganS - Here is the working example for your review.
Alphabetically_sort_list.qsf


MeganS - Here is the working example for your review.
Alphabetically_sort_list.qsf

 

why can’t I view the Alphabetically_sort_list.qsf file? It doesn’t lead to anything when I click on it.


MeganS - Here is the working example for your review.
Alphabetically_sort_list.qsf

 

why can’t I view the Alphabetically_sort_list.qsf file? It doesn’t lead to anything when I click on it.

The Community platform has changed since then, and some links in old messages didn’t survive.

BTW, you might be interested in the mcAlphabetize function. Unlike the code in this thread it works correctly (the code posted on this thread has a number of issues including sorting based on the browser language instead of the Qualtrics language). It also allows for anchored choices.


Leave a Reply