# Sort a feature table by geographic location

Here's in interesting challenge: I need to embed a table in a map layout so that the records in the table are sorted according to their geographic position on the ground. For example, in a north-to-top map, the northern-most feature (points in this case) should be first record, and the southern-most the last.

The map composition is in ArcGIS, but I'm willing to use gdal/ogr, qgis, etc. for the table sorting if need be.

To expand this question beyond my immediate use case and thereby make it more useful to the community, a robust solution would allow choosing other geographic sorts, such as west to east or inner to outer.

• What Coordinate System are you using? – Mapperz May 7 '13 at 18:05
• @mapperz, geographic decimal degrees, but it could easily be UTM or Albers if needed. – matt wilkie May 7 '13 at 18:22

To sort in the direction with a bearing of a degrees east of north, precompute the unit direction vector as (sin(a), cos(a)).

With a field calculation, obtain the (projected) [X] and [Y] coordinates of features (use their centroids or whatever for non-point features) if they aren't already available and compute a new field equal to the distance along the bearing, given by the dot product of the direction vector with the coordinates:

z = sin(a) * [X] + cos(a) * [Y]


Sort the table on [z] in ascending order.

For example, for a north-to-south sort the bearing is 180 degrees, v = (sin(180), cos(180)) = (0, -1), and the resulting sort therefore is on -[Y], which arranges the records from largest [Y] (first) down to smallest [Y] (last), exactly as intended.

An "inner to outer" sort could mean many things, but one interpretation is that the sorting should be by distance relative to a central location. A similar solution applies, using the Pythagorean theorem to compute (squared) distance from a fixed point. (Applying the square root is unnecessary because it does not change the sort order.)

A more fanciful sort order is explained and illustrated at "One-dimensional map of the world."

To express whuber's answer in terms of tool use, here's a simple implementation of the North-South, East-West use case when using point data in Arcgis:

• Add X and Y (or Lat and Long) columns to the attribute table, Calculate Geometry
• export to Excel or whatever, sort by the column of interest (X for East-West, Y for North-South), and then bring back in with Add X/Y data.

courtesy of Esri Technical Article HowTo: Sort features North/South or East/West for Export

How to change the order of features in a shapefile? has solutions to do the table sorting part for both Arcgis and Qgis without need to export to an external program (though you still need to duplicate the table/feature class), and Sorting shapefile records on attribute to update new rank field using Python? has an arcpy method.

• ArcGIS 10.0 and greater provides a Spatial Sort option in the Sort Tool. – klewis May 13 '13 at 17:16

ArcGIS 10.0 and greater provides a Spatial Sort option in the Sort Tool

The Sort tool help, which needs an Advanced level license, describes its spatial_sort_method which:

Specifies how features are spatially sorted. Sort method is only enabled when the Shape field is selected as one of the sort fields.
UR —Sorting starts at upper right corner. This is the default.
UL —Sorting starts at upper left corner.
LR —Sorting starts at lower right corner.
LL —Sorting starts at lower left corner.
PEANO —Sorting uses a space filling curve algorithm, also known as a Peano curve.