Sort logic changes the order in which respondents see a question's answer options, sub-questions, or entities. You can sort them alphabetically (a-z), in reverse alphabetical order (z-a), or in a custom order stored in an array.
Learn more about Sort logic in the Advanced Logic course in the Lighthouse Academy!
In This Article
- Using Sort Logic
- Sorting Alphabetically
- Exceptions to Sort Logic
- Sorting in a Custom Order with an Array
- Keeping the Same Random Order Across Questions
- Sorting for Some Respondents Only
- Which Questions Can Be Sorted
- Sorting, Randomization, and Anchored Options
Using Sort Logic
Type the sort command in square brackets in any question's text field. The command names the list it sorts, so you can put it on the sorted question itself or on any other question. Logic is usually easiest to find when it sits on the question it affects.
[sort Q2A by a-z]
The reference after sort says which list to reorder:
| Reference | What it sorts |
|---|---|
Q2A |
The answer options of Q2. |
Q2SQ |
The sub-questions (rows) of Q2, for example the statements in a Sliders question. |
Q2E |
The entities of Q2, for example the brands in a Topography question. |
PQ1A |
The answer options of custom prequalification question PQ1. |
Sorting changes only the order respondents see. Each item keeps its number, so Q2A3 still means the third option in the editor wherever it lands, and your logic and results refer to it the same way.
Sorting Alphabetically
There are two ways to write an alphabetical sort. Both do the same thing:
| Logic | What the respondent sees |
|---|---|
[sort Q2A by a-z] |
Q2's answer options from A to Z. |
[sort Q2A by z-a] |
Q2's answer options from Z to A. |
[sort by a-z Q2A] |
Short form: the same as [sort Q2A by a-z]. |
[sort by z-a Q3SQ] |
Q3's sub-questions from Z to A. |
The short form doesn't accept except. Use the long form (sort Q2A by a-z) when you need to exclude items.
How text is compared
- Upper and lower case count as the same letter. "apple" and "Apple" sort together.
- Formatting is ignored. Bold, italics, and other formatting don't affect the order.
- Text is compared one character at a time, including numbers. "10 years" comes before "2 years", because "1" comes before "2". To sort numbers by value, start them with zeros so they have the same number of digits ("02 years", "10 years").
- Digits and most punctuation come before letters. A leading space also counts as a character.
- Accented letters come after Z. "Éclair" sorts after "Zucchini". Keep this in mind when you sort lists in languages other than English.
Exceptions to Sort Logic
Checking the N/A box adds a system-generated N/A option, and sort logic always keeps it at the bottom of the list. To keep other options out of the sort (for example, "Other, please specify" or "None of the above"), add an except clause with the reference to exclude:
[sort Q3A by a-z except Q3A15]
An excluded item stays at its position from the editor. In the example above, A15 stays 15th and the other options are sorted around it. An option at the top of the editor list, such as A1, stays first.
-
Several items: list them with commas (
except Q3A1,15) or give a range (except Q3A14-15). Use either commas or a range in oneexcept, not both. -
Same question and level only: the excluded items must belong to the list being sorted.
[sort Q3A by a-z except Q4A2]or[sort Q3SQ by a-z except Q3A2]shows the error "In except section of sort, only question being sorted can be referenced". -
Long form only: write
[sort Q3A by a-z except Q3A15], not[sort by a-z Q3A except Q3A15].
Sorting in a Custom Order with an Array
To show items in an order of your own, store their numbers in an array, then sort by the array. The array lists item numbers in the order you want them shown.
| Logic | What the respondent sees |
|---|---|
In Q1: [CustomOrder << 2,3,1]In Q2: [sort Q2A by CustomOrder]
|
A2 first, then A3, then A1. |
In Q1: [CustomOrder << 4,3,2,1]In Q2: [sort Q2A by CustomOrder]
|
Q2's four options in reverse: A4, A3, A2, A1. |
In Q1: [CustomOrder << 4,3,2,1]In Q2: [sort Q2SQ by CustomOrder]
|
Q2's sub-questions in reverse. Use Q2E to order entities. |
In Q1: [CustomOrder << 1,2,3,4] [CustomOrder = shuffle CustomOrder]In Q2: [sort Q2A by CustomOrder]In Q3: [sort Q3A by CustomOrder]
|
Q2 and Q3 options in the same random order. Each respondent gets a different order. |
-
Unlisted items appear after the listed ones. With five options and
[CustomOrder << 4,3,2,1], A5 stays fifth. If the question also randomizes its options, the unlisted items come out in random order. - Repeated numbers count once. Numbers that match no item are ignored.
- N/A always stays at the end.
- Fill the array first: the array must be filled by a question the respondent answers before the sorted question. If the array is empty when the question is shown, the respondent sees the editor order.
⚠️ Note: The array holds the item numbers in display order, not the position each item moves to.
[CustomOrder << 2,3,1]shows A2, A3, then A1. A reversed list such as 4,3,2,1 looks the same either way, so test a non-symmetric order in Preview if you're unsure.
To learn how to create and fill arrays, see Variables and Arrays in aytm Logic. To sort the values inside an array itself ([x = sort scores]), see the aytm Logic Reference Guide.
Keeping the Same Random Order Across Questions
A common use of sort by array is to show the same list in several questions in a random order that differs between respondents but stays the same within each respondent's survey. For example, if a respondent sees "Hiking" first in Q2, they also see it first in Q3. This takes three steps.
Step 1: Create a reference array
In a question that respondents see before the questions you want to sort, add array logic listing the item numbers of every answer option or sub-question to include. Name the array after your list. In this example the array is called activities:
[activities << 1,2,3,4,5,6,7,8,9,10,11,12,13]
Write out every number, separated by commas. A range doesn't work here: 1-13 is read as 1 minus 13.
Step 2: Shuffle the array
Shuffle the numbers once per respondent. Place the shuffle after the line that creates the array, in the same or a later question, and before any question that uses the array:
[activities = shuffle activities]
Step 3: Sort each question by the array
In each question you want in that order, add sort logic that names the list and the shuffled array. Use A for answer options or SQ for sub-questions:
[sort Q2A by activities] [sort Q3SQ by activities]
Notes
- The questions don't need the Randomize option. If it's on, the sort still sets the order of every item listed in the array.
- If the array leaves out some options, they appear after the shuffled ones. With 8 options and
[brands << 1,2,4,5,6] [brands = shuffle brands], respondents see A1, A2, A4, A5, and A6 in a random order, then A3, A7, and A8 in editor order (or in random order, if the question randomizes its options). - The same array can also order paths in Group logic. See How to Control Group Logic with a Custom Array.
Sorting for Some Respondents Only
Add an if condition at the end of any sort command to sort only for respondents who meet it. Everyone else sees the usual order.
-
[sort Q4A by a-z if Q1A2]: sorts Q4 alphabetically only for respondents who chose A2 in Q1. -
[sort by z-a Q4A if Q1A2]: the short form with a condition. -
[sort Q4A by a-z except Q4A9 if Q1A2]:exceptgoes beforeif. -
[sort Q4A by CustomOrder if gender = "m"]: the custom order for male respondents only.
Which Questions Can Be Sorted
| List | Question types |
|---|---|
Answer options (A) |
Radio buttons, Checkboxes, Forms, Reorder (ranking), Side-by-Side, Progressive Matrix, Shelf Test, Ecommerce, and custom prequalification questions |
Sub-questions (SQ) |
Sliders, Star rating, Smiley rating, Distribute, Polarity Scale, Progressive Matrix |
Entities (E) |
Topography, Quadrant, Heatmap |
On any other question type, or on a list the type doesn't have (such as the scale points of a Sliders question), the sort command has no effect. Check the order in Preview after you add sort logic.
Sorting, Randomization, and Anchored Options
- Sort replaces randomization. If a question is set to randomize its options and also has sort logic, the sort runs after the randomization, so respondents see the sorted order.
-
Anchored options are sorted too. An option set to a fixed position in the editor stays put during randomization, but an alphabetical sort moves it. Add it to
exceptto keep it in place. -
N/A always stays last, whether or not you list it in
except.
💡 Tip: To keep a group of options together or show only some of a list, see aytm Logic: Randomization and Glue and Survey Flow Operations.