Welcome to the LimeSurvey Community Forum

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

Display sorted values

  • michaelji
  • michaelji's Avatar Topic Author
  • Offline
  • Junior Member
  • Junior Member
More
7 years 8 months ago #139674 by michaelji
Display sorted values was created 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
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
7 years 8 months ago - 7 years 8 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:

Code:
<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.
Last edit: 7 years 8 months ago by tpartner.
The following user(s) said Thank You: michaelji
The topic has been locked.
  • michaelji
  • michaelji's Avatar Topic Author
  • Offline
  • Junior Member
  • Junior Member
More
7 years 8 months ago #139819 by michaelji
Replied by michaelji on topic Display sorted values
Thanks, I will try this and report back
The topic has been locked.
More
5 years 1 month ago #181375 by marektondryk
Replied by marektondryk on topic Display sorted values
How can I do the same with 'radio type questions'?
The topic has been locked.
More
5 years 1 month ago #181466 by marektondryk
Replied by marektondryk on topic Display sorted values
Tony, why Short-text question not Equation question?
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
5 years 1 month ago - 5 years 1 month 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.
Last edit: 5 years 1 month ago by tpartner.
The topic has been locked.
More
5 years 1 month 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?
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
5 years 1 month 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.
The topic has been locked.
More
5 years 1 month ago - 5 years 1 month 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
Last edit: 5 years 1 month ago by marektondryk.
The topic has been locked.

Lime-years ahead

Online-surveys for every purse and purpose