## `stats::sortSample` -- sort the rows of a sample

#### Introduction

`stats::sortSample``(s, ..)` sorts the rows of the sample `s`.

#### Call(s)

`stats::sortSample(s)`
`stats::sortSample(s, c1, c2, ..)`
`stats::sortSample(s, [c1, c2, ..])`

#### Parameters

`s` | - | a sample of domain type `stats::sample`.
`c1, c2, ..` | - | integers representing column indices of the sample `s`.

#### Returns

a sample of domain type `stats::sample`.

#### Related Functions

`stats::selectRow`

#### Details

- The sorting of rows only uses the entries of the specified columns. First, rows are sorted according to the elements of the first specified column. Those rows with identical elements in the first specified column are then ordered according to the elements in the second specified column etc.
- If no columns are specified, then column 1 is used for sorting. In case of a tie, column 2 is used etc.
- Numbers are sorted numerically, strings are sorted lexicographically. Identifiers are sorted according to the strategy used by MuPAD's `sort` command. Identifiers come first, numbers second.

#### Example 1

We create a sample with one column and sort it:

    >> stats::sortSample(stats::sample([x, g2, 3, g1, 8/5, 2]))

                                         x
                                        g1
                                        g2
                                       8/5
                                         2
                                         3

#### Example 2

We create a sample with two columns:

    >> stats::sample([[b, 2], [a, 5], [a, 2], [c, 1], [b, 3]])

                                      b  2
                                      a  5
                                      a  2
                                      c  1
                                      b  3

Note the different sorting priorities specified by the column indices:

    >> stats::sortSample(%, 1), stats::sortSample(%, 2),
       stats::sortSample(%, 1, 2), stats::sortSample(%, 2, 1)

                          a  2  , c  1  , a  2  , c  1
                          a  5    a  2    a  5    a  2
                          b  3    b  2    b  2    b  2
                          b  2    b  3    b  3    b  3
                          c  1    a  5    c  1    a  5

#### Example 3

We create a sample containing income and costs in the years 1997 and 1998:

    >> stats::sample([[123, "costs", "97"], [720, "income", "98"],
                      [623, "income", "97"], [150, "costs", "98"]])

                               123  "costs"   "97"
                               720  "income"  "98"
                               623  "income"  "97"
                               150  "costs"   "98"

We sort according to the year (third column):

    >> stats::sortSample(%, 3)

                               623  "income"  "97"
                               123  "costs"   "97"
                               150  "costs"   "98"
                               720  "income"  "98"

We sort with priority on the year. Items of the same year are then sorted lexicographically (``costs'' before ``income''):

    >> stats::sortSample(%2, 3, 2)

                               123  "costs"   "97"
                               623  "income"  "97"
                               150  "costs"   "98"
                               720  "income"  "98"

#### Changes

- new function

Do you have questions or comments?
Copyright © SciFace Software GmbH & Co. KG 2000