#### A Decreasing Sort

Sorting in Stata is reasonably efficient - a million random
values can be put in increasing order in <3 seconds with

sort x
However the sort command doesn't have a inverse option to sort
from high to low. You might be led to the -gsort- command:
gsort -x
but that does a decreasing sort in an inefficient manner - it
sorts increasing on x, then sorts increasing on minus _n.
Essentially it is doing:
sort x
gen long sortvar = -_n
sort sortvar
drop sortvar
That works, but it takes about 3 times as long as just sorting
on x because sorting on -_n is the worst case sort. Better to
negate x yourself before an increasing sort:
generate negx = -x
sort negx
which is almost as fast as the increasing sort. There is no
similar solution for a decreasing sort on an alphabetic
variable.
Also recall that there are no promises about the order
of records within a group a records that match on the sort
criteria, unless you add the -stable- option.

Although there is a Statalist posting claiming otherwise, it
appears to me that the only additonal memory required for a sort
is for a list of pointers to the records - probably 8 bytes times
the number of obervations.

Daniel Feenberg