Stata 15 help for _qsort_index

Title

[P] _qsort_index -- Sort a list of words

Syntax

_qsort_index list1 [\ *|list2 [\ ...]] [, options ]

options Description ------------------------------------------------------------------------- ascending sorts in ascending order (default) descending sorts in descending order alpha sorts alphabetically; default is numerically display displays the sorted list (for debugging) -------------------------------------------------------------------------

Description

_qsort_index is a programming utility that sorts the words in list1 in ascending or descending order, and applies the same permutation to the words of list2, list3 etc. All specified lists should have the same number of words, n. * is expanded to the list 1..n.

The ordering may be in numeric or alphanumeric order.

The ordering is returned in r(order). The ordered version of listj is returned as r(slistj).

If you are looking to sort a list of numbers, and are not interested in the sorting order, use the extended macro function :list sort (see macrolists), which is much faster.

Currently quoted string elements of lists are not fully supported. For instance, with _qsort_index `"y "m n o" x "a b c" z"', alpha, the r(order) will be correct, while r(slist1) is in the correct order, but with internal quotes stripped.

Options

ascending and descending specify that the words in list1 are sorted in ascending (increasing) or descending (decreasing) order. In accordance with sorting observations, _qsort_index defaults to ascending.

alpha specifies that the words are compared alphanumerically. The default is to compare the words numerically.

display displays the sorted lists. This option is mostly useful while debugging.

Remarks

Beware about sorting numerical lists with missing values (., .a, .b, ... .z). Missing values are located last and ordered in increasing order even if option descending was specified.

_qsort_index is usually stable, that is, the order of tied values remains the same as the input order. The exception is lists with tied values, sorted alphanumerically in descending order.

Examples

the most common application of _qsort_index

. _qsort_index 3 1 5 4 2

then: r(order) : "2 5 1 4 3" r(slist1) : "1 2 3 4 5"

sort multiple lists in the order of the first list

. _qsort_index 3 1 5 4 2 \ 1 4 9 16 25 \ *, descending

then: r(order) : "3 4 1 5 2" r(slist1) : "5 4 3 2 1" r(slist2) : "9 16 1 25 4" r(slist3) : "3 4 1 5 2"

. _qsort_index 3 1 5 4 2 \ a b c dd ev, ascending

then: r(order) : "2 5 1 4 3" r(slist1) : "1 2 3 4 5" r(slist2) : "b ev a dd c"

_qsort_index uses a "stable" sorting algorithm:

. _qsort_index 1 3 2 3 1 \ a b c d e

then: r(order) : "" r(list1) : "1 1 2 3 3" r(list2) : "a e c b d"

To obtain an alphabetically ordered list of variables

. unab vlist: _all unabbreviated varlist in vlist . _qsort_index `vlist', alpha alpha-sorted varlist in r(list1)


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index