Desktop User Guides > Professional > Table scripting > Table presentation > Sorting tables > Introduction to sorting tables
 
Introduction to sorting tables
Sample script file: MyFirstSortedTable.mrs
This example script is based on the Museum sample data set. See Running the sample table scripts for more information.
Sometimes it is useful to sort your tables, so that you can see at a glance which is the most important or popular response. Sorting tables is sometimes called ranking. To understand how it works, first consider an unsorted table of Remember by Biology:
This graphic is described in the surrounding text.
Suppose you want to sort the table so that the rows that contain more respondents appear before those containing fewer respondents. When you sort the rows, you need to select the column on which you want to sort. To do this, use the Table.SortColumn property. The following example sorts the rows in descending order of the values in the base column:
With TableDoc.Tables
.AddNew("Table2", "Remember * Biology", "Remember by Biology - Rows Sorted")
.Table2.SortColumn = "Biology{Base}"
End With
Now look at the table:
This graphic is described in the surrounding text.
Compare this table with the unsorted table: the rows have been reordered, making it easy to see which categories were most popular. Although you generally want to sort the rows on the base column, you are not restricted to doing this. For example, you can sort the rows on the values in the Yes column:
With TableDoc.Tables
  .AddNew("Table3", "Remember * Biology",
        "Remember by Biology -   Rows Sorted on Yes Column")
  .Table3.SortColumn = "Biology{Yes}"
End With
You set the Table.SortColumn property to the axis name followed by the element name enclosed in braces { }. Make sure that the axis and element names are correct and actually exist on the top of the table. (For information about specifying the sort column in a nested table, see Sorting nested and concatenated tables.)
Here is the table produced by the script:
This graphic is described in the surrounding text.
The order of the rows has changed to reflect the values in the Yes column.
You can sort the columns, just like you can sort the rows. When you sort the columns, you need to specify the row on which you want to sort. For example:
With TableDoc.Tables
  .AddNew("Table4", "Remember * Biology",
          "Remember by Biology - Columns Sorted on Base Row")
  .Table4.SortRow = "Remember{Base}"
End With
Here is the table produced by the script:
This graphic is described in the surrounding text.
The order of the Yes and No columns has been reversed, making it easy to see that more respondents chose the No category.
And of course, you can sort both rows and columns in the same table:
With TableDoc.Tables
  .AddNew("Table5", "Remember * Biology",
          "Remember by Biology - Rows and Columns Sorted")
  .Table5.SortRow = "Remember{Base}"
  .Table5.SortColumn = "Biology{Base}"
End With
Here is the table produced by the script:
This graphic is described in the surrounding text.
You can sort a one-dimensional table by specifying the sort row or column as “Base”. For example:
With TableDoc.Tables
  .AddNew("Table6", "Remember",
          "Interest rating for the Remember gallery")
  .Table6.SortColumn = "Base"
End With
Here is the table produced by the script:
This graphic is described in the surrounding text.
You can also sort tables on the first or last row or column in the table, without referring to a specific row or column name. To so this, you use the appropriate line from the following syntax:
Table.SortRow = "first"
Table.SortColumn = "last"
Table.SortColumn = "first"
Table.SortColumn = "last"
This can be useful if you are running tracking studies to monitor change over time, as you can reuse the same table with each wave of data without having to re-select the row or column to sort on. For example, if you have a table containing monthly data, you might use the syntax Table.SortColumn = "last" to sort the table by the latest month, rather than selecting the name of the month each time you update the data.
By default, the sort is always carried out in descending order. However, you can sort tables in ascending order by specifying the sort order for the row or column, for example:
With TableDoc.Tables
  .AddNew("Table7", "Remember",
      "Interest rating for the Remember gallery, sort ascending")
  .Table7.SortColumn = "Base"
  .Table7.SortColumn.Order = 0 ' Ascending order
End With
Here is the table produced by the script:
This graphic is described in the surrounding text.
All of the tables in this topic have only one item of cell contents. When the table contains more than one item of cell contents, the sort is always based on the first item of cell contents. When creating sorted tables that contain more than one item of cell contents, you therefore must consider the order in which you specify the items of cell contents.
For information on sorting concatenated and nested tables, grid tables, and special elements, see Sorting tables.
Requirements
UNICOM Intelligence Reporter
See also
Sorting tables