*! version 1.0.2 23Jan2019

Title

hashsort -- sort and gsort using hashes and C-plugins

Important: Please run gtools, upgrade to update gtools to
the latest stable version.

Syntax

hashsort
[+|-]
varname
[[+|-]
varname ...]
[, options]

Menu

Description

hashsort uses C-plugins to implement a hash-based sort that is always
faster than sort for sorting groups and faster than gsort in
general. hashsort hashes the data and sorts the hash, and then it sorts
one observation per group. The fewer the number of gorups relative to the
number of observations, the larger the speed gain.

If the sort is expected to be unique or if the number of groups is large, then
this comes at a potentially large memory penalty and it may not be faster than
sort (the exception is when the sorting variables are all integers).

Each varname can be numeric or a string. The observations are placed in
ascending order of varname if + or nothing is typed in front of the
name and are placed in descending order if - is typed. hashsort
always produces a stable sort.

hashsort is part of the [R] gtools project.

Options

Options

genenerate(varname) Store group ID in generate.

sortgen Set data sortby variable to generate.

replace If generate exits, it is replaced.

skipcheck Skip internal is sorted check.

Gtools

compress Try to compress strL to str#. The Stata Plugin Interface
has only limited support for strL variables. In Stata 13 and earlier
(version 2.0) there is no support, and in Stata 14 and later (version
3.0) there is read-only support. The user can try to compress strL
variables using this option.

forcestrl Skip binary variable check and force gtools to read strL
variables (14 and above only). Gtools gives incorrect results when there is binary data in strL variables.
This option was included because on some windows systems Stata detects
binary data even when there is none. Only use this option if you are
sure you do not have binary data in your strL variables.

verbose prints some useful debugging info to the console.

benchmark and benchmarklevel(int) print how long in
seconds various parts of the program take to execute. The user can also
pass bench(int) for finer control. bench(1) is the same
as benchmark but bench(2) and bench(3) additionally print
benchmarks for internal plugin steps.

hashmethod(str) Hash method to use. default automagically
chooses the algorithm. biject tries to biject the inputs into the
natural numbers. spooky hashes the data and then uses the hash.

oncollision(str) How to handle collisions. A collision should never
happen but just in case it does gtools will try to use native commands.
The user can specify it throw an error instead by passing oncollision(error).

Examples

Also see the
online documentation
for more examples.

Setup

. sysuse auto

Place observations in ascending order of price

. hashsort price

Same as above command

. hashsort +price

List the 10 lowest-priced cars in the data

. list make price in 1/10

Place observations in descending order of price

. hashsort -price

List the 10 highest-priced cars in the data

. list make price in 1/10

Place observations in alphabetical order of make

. hashsort make

List make in alphabetical order

. list make

Place observations in reverse alphabetical order of make

. hashsort -make

List make in reverse alphabetical order

. list make

Setup

. webuse bp3

Place observations in ascending order of time within ascending
order of id

. hashsort id time

List each patient's blood pressures in the order measurements were
taken

. list id time bp

Place observations in descending order of time within ascending
order of id

. hashsort id -time

List each patient's blood pressures in reverse-time order

. list id time bp

Author

Mauricio Caceres Bravo

mauricio.caceres.bravo@gmail.com

mcaceresb.github.io

Website

hashsort is maintained as part of [R] gtools at github.com/mcaceresb/stata-gtools

Acknowledgment

This help file was based on StataCorp's own help file for sort and gsort.

This project was largely inspired by Sergio Correia's ftools:
https://github.com/sergiocorreia/ftools.

The OSX version of gtools was implemented with invaluable help from @fbelotti;
see https://github.com/mcaceresb/stata-gtools/issues/11.

Also see

help for
gtools;
fsort (if installed),
ftools (if installed)