Display Answer Choices in Alpha Order | XM Community
Solved

Display Answer Choices in Alpha Order

  • 10 June 2020
  • 9 replies
  • 417 views

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!

icon

Best answer by SurajK 12 June 2020, 22:17

View original

9 replies

Userlevel 5
Badge +4

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

Userlevel 5
Badge +4

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?

Userlevel 5
Badge +4

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 :(

Userlevel 5
Badge +4

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 :(

Userlevel 5
Badge +4

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

Leave a Reply