Sorting a Table

About the Sort Function

By default, a table displays data in the order that it appears in your data set. You can change the order by sorting the data based on the values of one or more variables.
Here are some characteristics of the sort function:
  • The default behavior is to sort alphanumerically in ascending order (A to Z, zero to 9, or earliest to latest date). You can change the order when you perform a sort.
  • Your sort can be based on more than one variable. For example, suppose that the data contains a variable for last name and another variable for first name. You can use last name as the primary sort and first name as the secondary sort. The last name variable has the higher priority in the sort.
  • You can use a specialized order type to use in a sort. For example, if your data contains a category variable, you might want to sort by frequency of the variable’s occurrences rather than sort alphabetically. For more information, see Using a Specialized Sort Order.
The sort order remains in effect only while the current project is open. If you close and then reopen the project, the sort order reverts to the default order.

Sort a Table

To sort a table:
  1. (Optional) Select one or more variables to use in the sort.
  2. Select Table Viewthen selectVariablesthen selectSort. The Sort dialog box opens. Note the following details about this dialog box:
    • If any variables were selected when you opened the dialog box, then those variables appear in the Sort by list.
    • Any variables that appear in the Sort by list are used in the sort.
    • The order in which variables are listed determines their sort priority.
    • In the Sort by list, an A or D precedes each variable name. These letters indicate whether the sort order is ascending or descending, respectively.
    • If an asterisk appears next to the A or D, then a specialized sort order has been specified for the variable. For details, see Using a Specialized Sort Order.
  3. You can perform any of the following actions in the Sort dialog box:
    • To sort by a variable, select the variable in the Variables list and click Add variable to sort list. The variable is added to the Sort by list.
    • To remove a variable from the Sort by list, select the variable and click Remove variable from sort list.
    • To change the variable’s ascending or descending sort status, select the variable in the Sort by list and click Ascending or Descending. You can also double-click the variable’s name in the list.
    • To change the sort priority of a variable, select the variable in the Sort by list and click Up or Down.
  4. Click OK.

Using a Specialized Sort Order

Specialized Sort Order Overview

By default, the sort order is based on the alphanumeric value of the sort variable. You can specify other factors to use for a sort. For example, you can sort by frequency of a variable occurrence.
The sort function behaves differently depending on whether you have associated a format with the variable used for the sort. For example, suppose that an MMDDYY10 format has been applied to a date variable, and you choose to sort the formatted values rather than the numerical date values. In your results, sorted in ascending order, a date of 02/05/1982 comes before 07/07/1969 because 02 comes before 07.
The following figure shows a formatted sort on birthdate values.
Table Sorted by Formatted Dates
A format sort on birthdate values
For some sort orders, application of a format produces the same result as no format. In the previous example, if you perform a frequency sort on the date variable, then the formatted results are the same as the non-formatted results. A date that appears three times in the data set also appears three times in the sorted table regardless of whether the date is formatted.
The following figure shows the frequency of birthdate values formatted as MMDDYY10. The sort order is the same for non-formatted values.
Table Sorted by Frequency of Formatted Dates
Frequency of birthdate values formatted as mmddyy10.
However, if you apply a format such as DOWNAME to the variable, then the results differ. In this case, you might see three observations grouped together with a Friday date, three observations for Tuesday, and so on.
The following figure shows a frequency sort on the same data, but with birthdate values formatted as DOWNAME. In this case, the format affects the sort order.
Table Sorted by Frequency, Dates Formatted as DOWNAME
Frequency of birthdate values formatted as downame.

Use a Specialized Sort Order

To use a specialized sort order:
  1. Select the variable that you want to use for your sort.
  2. Right-click on the variable, select Ordering from the pop-up menu, and then select one of the ordering options shown in the following table:
    Option
    Type of Sort
    Applies Format1
    Standard
    This option is the typical alphanumeric sort. This option is the only ordering available for interval variables. If necessary, change the variable’s measurement level to nominal. See Change a Variable’s Measurement Level.
    no
    by Format
    Sort order is based on the variable’s format. For example, if the data contains a date variable formatted as MMDDYY10., the formatted values are sorted rather than the numerical date values.
    yes
    by Frequency
    Sort order is based on the frequency of variable values.
    yes
    by Data
    Data items are collated and sorted by the order in which data appears in the selected variable of the data set.
    yes
    by Frequency (unformatted)
    Sort order is based on the frequency of variable values.
    no
    by Data (unformatted)
    Data items are collated and sorted by the order in which data appears in the selected variable of the data set.
    no
    Custom
    This option is not used and is therefore dimmed.
    not applicable
    1The Applies Format column is relevant only if you have associated a format with the variable used for the sort. This column indicates whether the sort operation is performed on the formatted value (Yes) or on the raw numeric values (No).
  3. Sort the table. For instructions, see Sort a Table. When you sort the table, your specialized sort order is used.