Display sorted values

More
2 years 9 months ago #139674 by michaelji
I would like to know if I can display in the admin email of a text question a sorted list of the greatest to smallest vales of the questions.

Q1 1 2 3 4 5 answered 3
Q2 1 2 3 4 5 answered 4
Q3 1 2 3 4 5 answered 2
Q4 1 2 3 4 5 answered 5
Q5 1 2 3 4 5 answered 1

I should show that the order of the answers is: Q4, Q2, Q1, Q3, Q5

Thanks

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

LimeSurvey Partners
More
2 years 9 months ago - 2 years 9 months ago #139708 by tpartner
Replied by tpartner on topic Display sorted values
As far as I know, there is no way to do that with Expression Manager, however, you could use JavaScript to load a hidden short-text question with a sorted array of the question codes. This hidden question would need to be placed in a group following that of Q1-Q5.

You can then insert the value of the hidden question into the admin email.

Since there is no sorting of associative arrays in JavaScript, we will need to build an array of arrays containing the answer:qCode pairs and sort that. Then we can loop through the sorted array, building a new array of the qCodes. That array can be written into the question input.

Insert this script into the source of the short-text question:

<script type="text/javascript" charset="utf-8">	
 
    $(document).ready(function() { 	
 
		// Uncomment the line below to hide the question
		// $('#question{QID}').hide();
 
		// Build an associative array of the answers
		var answers = {
			Q1:{Q1}, 
			Q2:{Q2}, 
			Q3:{Q3}, 
			Q4:{Q4}, 
			Q5:{Q5}
		}
 
		// Build an array of arrays with the qCode:answer pairs
		var sortedAnswers = [];		
		for(var question in answers) {
			sortedAnswers.push([question, answers[question]]);
		}
 
		// Sort that array of arrays descending
		sortedAnswers.sort(
			function(a, b) {
				return b[1] - a[1]
			}
		);
 
		// Build an array of the sorted qCodes
		var sortedQuestions = [];		
		$(sortedAnswers).each(function(i) {
			sortedQuestions.push(this[0]);
		});
 
		// Load the sorted qCodes into the question input
		$('#question{QID} input.text').val(sortedQuestions);
	});
 
</script>

Sample survey attached:

File Attachment:

File Name: limesurvey...-2-3.lss
File Size:25 KB

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Official LimeSurvey Partner - partnersurveys.com
Attachments:
Last edit: 2 years 9 months ago by tpartner.
The following user(s) said Thank You: michaelji

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

More
2 years 9 months ago #139819 by michaelji
Replied by michaelji on topic Display sorted values
Thanks, I will try this and report back

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

More
2 months 2 weeks ago #181375 by marektondryk
Replied by marektondryk on topic Display sorted values
How can I do the same with 'radio type questions'?

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

More
2 months 1 week ago #181466 by marektondryk
Replied by marektondryk on topic Display sorted values
Tony, why Short-text question not Equation question?

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

More
2 months 1 week ago - 2 months 1 week ago #181472 by tpartner
Replied by tpartner on topic Display sorted values
You cannot directly load an equation question via JavaScript.

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Official LimeSurvey Partner - partnersurveys.com
Last edit: 2 months 1 week ago by tpartner.

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

More
2 months 1 week ago #181479 by marektondryk
Replied by marektondryk on topic Display sorted values
How can I display sorted values? {QHidden} in next Text display question? How display it in e-mail?

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

More
2 months 1 week ago #181481 by tpartner
Replied by tpartner on topic Display sorted values
I don't understand the question. You simply place the question code in curly braces as you have indicated.

Did you actually try the sample survey?

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Official LimeSurvey Partner - partnersurveys.com

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

More
2 months 1 week ago - 2 months 1 week ago #181497 by marektondryk
Replied by marektondryk on topic Display sorted values
Yes, here is modified survey...

File Attachment:

File Name: limesurvey...5668.lss
File Size:29 KB


There is no sorted values in question Q7
Attachments:
Last edit: 2 months 1 week ago by marektondryk.

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!