๐Ÿ‡ฎ๐Ÿ‡น Italiano | ๐Ÿ‡ฌ๐Ÿ‡ง English

GeoSort โ€” Complete Guide

Sort features of a QGIS vector layer by geometric and attribute criteria โ€” 18 criteria, hierarchical multi-criterion sorting, examples and use cases.

Version 1.11.1 QGIS 3.16+ | 4.x GPLv2 license

Introduction

GeoSort is a QGIS plugin that sorts the features of a vector layer (points, lines or polygons) by a chosen geometric or attribute criterion, and writes a progressive field (default sort_order: 1 = first feature in the chosen order) to the layer or to an in-memory copy. It is available both as an interactive dialog (Vector โ†’ GeoSort menu) and as a Processing Toolbox algorithm, usable in batch, in the graphical modeler and via headless PyQGIS.

GeoSort dialog with the available sort criteria
The GeoSort dialog: input layer, sort criterion, options and output in a single non-modal window.

This guide explains every available criterion, its parameters (in the dialog and in Processing), when it's worth using and a typical use case with a before/after screenshot. For a compact table summary see also the project README.

Installation

  1. Download the plugin .zip from the releases page.
  2. In QGIS: Plugins โ†’ Manage and Install Plugins โ†’ Install from ZIP.
  3. Select the geosort.zip file.
  4. The plugin appears under Vector โ†’ GeoSort and in the Processing Toolbox under GeoSort.
No external dependencies: GeoSort only uses PyQGIS APIs and the Python standard library. It works on both QGIS 3.16+ (Qt5) and QGIS 4.x (Qt6) with the same .zip file.

Basic concepts

  • Progressive field โ€” GeoSort writes an integer field (default sort_order, name/start value/step all customizable โ€” see Custom numbering) with each feature's position in the chosen order: 1 = first feature.
  • Direction โ€” Ascending or Descending, applies to any criterion.
  • NULL values โ€” for attribute/expression criteria, choose whether features with a NULL value go first or last.
  • NULL or incompatible geometries โ€” features with no geometry, or with a type incompatible with the chosen criterion (e.g. points with an "area" criterion), don't break the sort: they are pushed to the bottom.
  • Preview โ€” the Previewโ€ฆ button opens a popup with the first sorted features (FID, sort_order value, criterion value) to check the result before applying changes.

Attribute / Expression

Table attribute all types

Sorts by the value of any field on the layer (text, integer, float, date).

Dialog: "By attribute / expression" radio + field combo  โ€ข  Processing: CRITERION=0, ATTRIBUTE_FIELD

When to use it: the most direct criterion โ€” street name, cadastral code, category, survey date, any column already present in the attribute table.

QGIS expression all types

Sorts by the result of an arbitrary QGIS expression, the same syntax as the Field Calculator. Click the ฮต button to open the expression editor; the active expression is highlighted in green, with a Remove button to go back to a single field.

Dialog: "By attribute / expression" radio + expression editor  โ€ข  Processing: CRITERION=16, EXPRESSION

  • Combining fields: "area_kmq" / "pop"
  • Geometric functions: length($geometry), area($geometry)
  • Conditions: CASE WHEN "type"='A' THEN 1 ELSE 2 END
  • Concatenation for composite keys: "fid" || "id_poly"

When to use it: when the criterion isn't a single field but a combination, an on-the-fly geometric calculation, or a conditional classification.

Text sort modes

For both criteria above, a text value can be compared in two ways:

ModeBehaviorExample
Lexicographic (default)character-by-character comparison"1010" < "11" < "1111"
Natural Sortembedded digit sequences are compared as integers"11" < "1010" < "1111"

Useful with alphanumeric fields like FILE1, FILE2, FILE10 โ€” with lexicographic order FILE10 would end up before FILE2.

Lexicographic ordering of codes FILE1, FILE2, FILE10
Lexicographic: FILE1 โ†’ FILE10 โ†’ FILE2
Natural Sort of codes FILE1, FILE2, FILE10
Natural Sort: FILE1 โ†’ FILE2 โ†’ FILE10
Use case: a map-sheet layer named SHEET_1โ€ฆSHEET_120: without Natural Sort, SHEET_100 would appear right after SHEET_1.

Centroid coordinates

Centroid โ€” X / Y coordinate all types

Sorts by the geographic position of the geometry's centroid, along the X axis (westโ†’east) or Y axis (southโ†’north).

Processing: CRITERION=1 (X) / CRITERION=2 (Y)

When to use it: a progressive "left to right" or "bottom to top" numbering โ€” poles, manholes, trees along a dominant axis.

Centroid โ€” distance from reference point all types

Sorts by the Euclidean (or geodesic, see Geodesic measurement) distance of the centroid from a configurable reference point. In the dialog the point is set via the X/Y spin boxes or by clicking Pick point on map โ€” the dialog lowers (without closing) and the next click on the map canvas captures the coordinates; Esc cancels the pick.

Dialog: X/Y spin boxes + map picker  โ€ข  Processing: CRITERION=3, REF_POINT (optional, format "x,y [EPSG:xxxx]"; empty = origin 0,0)

Use case: sort maintenance interventions by increasing distance from an operations center, or the stations of a radial network by distance from the central node.
Points ordered by increasing distance from a reference point
Radial ordering: increasing distance from a reference point picked on the map.

Geometric property

Area / Perimeter polygons

Sorts polygons by surface area or perimeter length.

Processing: CRITERION=4 (area) / CRITERION=5 (perimeter)

Use case: cadastral parcels or planning zones sorted by descending area, to prioritize the largest units in a report.

Length lines

Sorts lines by total length.

Processing: CRITERION=6

Use case: road segments or channels sorted from shortest to longest before a batch geometry-simplification job.

Vertex count all types

Sorts by the number of vertices in the geometry.

Processing: CRITERION=7

Use case: quickly spot the most complex geometries (candidates for simplification) by sorting by descending vertex count.

Bounding Box โ€” width / height / area / Xmin / Ymin all types

Five variants on the size and position of the geometry's bounding box: width, height, area, minimum X coordinate, minimum Y coordinate.

Processing: CRITERION=8 (width), 9 (height), 10 (area), 11 (Xmin), 12 (Ymin)

Use case: sort clip extents or grid tiles by Xmin/Ymin when a simple row-by-row scan order is needed (for a more natural "serpentine" order see Serpentine instead).
Polygons ordered by descending area
Parcels sorted by area, from largest to smallest.

Distance from line

Distance from reference line all types

Sorts features by perpendicular distance from a reference line (a second, line-type layer), in two modes:

  • Distance from centroid โ€” distance between the feature's centroid and the line
  • Distance from element โ€” distance from the nearest point of the whole geometry to the line

Dialog: line-layer combo + mode  โ€ข  Processing: CRITERION=14, REF_LAYER, LINE_DISTANCE_MODE (advanced). The reference layer is automatically reprojected if its CRS differs from the input layer's.

Use case: buildings sorted by increasing distance from a road or from the coastline, to prioritize the closest units in a field survey.
Buildings ordered by increasing distance from a reference road
Distance from line, "element" mode: increasing order from the reference road.

Position along line

Position along reference line all types

Sorts features by their projected position along a reference line (a mileage-marker style progression), in three modes:

  • Centroid projection โ€” all features โ€” every feature is projected onto the line and sorted by the resulting position, even if it doesn't intersect it
  • Intersecting only โ€” via projected centroid โ€” as above, but only features that intersect the line; the others are excluded from the sort
  • Intersecting only โ€” via first contact point โ€” as above, but the position used is that of the first intersection point, not the projected centroid

Dialog: line-layer combo + mode  โ€ข  Processing: CRITERION=13, REF_LAYER, LINE_MODE (advanced)

Use case: streetlights, mileage markers or power-line supports numbered progressively along the route of a road, railway or power line.
Poles numbered progressively along a reference line
Position along line: progressive numbering of poles along the route.

Hilbert curve

Hilbert curve (spatial sorting) all types

Sorts features along a Hilbert curve computed on centroids, normalized to the layer's overall extent: features that are close in space stay close in the resulting order too โ€” unlike a row-based sort (X or Y) which can produce long jumps between features that are spatially close but at opposite ends of a row.

Processing: CRITERION=15, advanced parameter HILBERT_ORDER (default 16, grid resolution โ€” you almost never need to change it)

Not available as the primary criterion in multi-criterion mode: it requires the extent computed over all features of the layer before an order can be assigned.
Use case: writing a GeoPackage with spatially coherent features for faster reads (bounding-box queries), or ordering the plates of a "continuous path" atlas with no jumps between consecutive pages.
Points ordered along a Hilbert curve
Hilbert curve: the order follows a spatially continuous path, with no long jumps.

Serpentine (boustrophedon)

Serpentine all types

Sorts features into bands, with the cross axis alternating ascending/descending from one band to the next โ€” the classic ordering for numbering the plates of a regular map series or a photogrammetric flight path, without the long jump from the end of one band to the start of the next that a simple row-based sort produces.

  • Band orientation โ€” horizontal (by Y, X alternating โ€” default) or vertical (by X, Y alternating)
  • Band size โ€” height (horizontal) or width (vertical) of each band, in the layer's CRS units; 0/empty = automatic, from the average bounding-box size of the features
  • Starting corner โ€” combining the general Direction (which band comes first) with First band ascending (which way the cross axis runs in the first band) freely selects any of the four corners of the grid as the starting point

Dialog: orientation combo + band-size spin box + "First band ascending" checkbox  โ€ข  Processing: CRITERION=17, BAND_AXIS, BAND_SIZE, CROSS_ASCENDING (all advanced)

Not available as the primary criterion in multi-criterion mode: it requires the bands computed over all features.
Use case: numbering the plates of a regular map series, or planning the acquisition order of a photogrammetric flight, following the aircraft's real path (forward on one band, back on the next) instead of restarting from the opposite edge every time.
Grid of plates ordered in a serpentine pattern
Horizontal serpentine: bands by Y, X alternating โ€” no jump between the end of one band and the start of the next.

Hierarchical multi-criterion sorting

Set a secondary criterion to break ties in the primary criterion. Each level has its own direction (ascending/descending).

Example: primary = Region (ascending), secondary = Area (descending). Features are grouped by region and, within each group, sorted from largest to smallest area.
Polygons grouped by region and sorted by descending area within each group
Multi-criterion: region (asc.) then area (desc.) โ€” two-level hierarchical order.

Dialog: "Secondary criterion" combo  โ€ข  Processing: SECONDARY_CRITERION, SECONDARY_FIELD, SECONDARY_EXPRESSION, SECONDARY_DIRECTION (all advanced)

The secondary criterion is ignored when the primary criterion is based on a reference line (position/distance along line), the Hilbert curve, or the serpentine.

Geodesic measurement (geographic CRS)

On a layer with a degree-based CRS (e.g. EPSG:4326), planar area and length measurements are expressed in degreesยฒ/degrees โ€” metrically meaningless values that can skew the sort order by latitude. GeoSort can automatically compute area, perimeter, length and distances on the ellipsoid (geodesic measurement, in mยฒ or m).

ModeBehavior
Auto (default)geodesic measurement is enabled automatically when the CRS is geographic; planar measurements are kept on projected CRS. A non-blocking warning signals the automatic activation.
Alwaysgeodesic measurement always enabled, regardless of the CRS.
Neveralways planar, regardless of the CRS.

Criteria affected: polygon area, polygon perimeter, line length, centroid distance from reference point, distance from reference line.

Bounding box criteria and position along line always stay planar: the bounding box is a concept expressed in native coordinates, and position along line is monotonic and scale-invariant.

Dialog: "Measurement on geographic CRS" combo  โ€ข  Processing: advanced parameter GEODESIC (Auto/Always/Never)

Custom numbering

The progressive field is configurable along three axes, both in the dialog and in Processing:

ParameterDefaultExample
Field namesort_orderrank, order โ€” if the field already exists on the layer, it's overwritten instead of duplicated
Start value10 for zero-based numbering
Step110 โ†’ 10, 20, 30โ€ฆ, to leave room for future insertions

Processing: advanced parameters ORDER_FIELD, START, STEP

Optionally, GeoSort can also add a sort_value field with the raw value of the criterion used (e.g. the area, the distance) โ€” "Add field with the criterion value used" checkbox in the dialog, ADD_VALUE_FIELD in Processing.

Selected features only

  • With the Sort selected features only checkbox, the sort considers only the features currently selected on the layer; in Update current layer mode, the sort_order field is written only on those features.
  • In the Processing Toolbox, the same behavior is offered by QGIS's native Selected features only toggle on the input layer (QgsProcessingFeatureSourceDefinition(..., selectedFeaturesOnly=True) from PyQGIS).

Output

  • Update current layer โ€” adds or updates the progressive field (default sort_order). If the field already exists, confirmation is requested before overwriting it. The write is wrapped in a single undo command, undoable with Ctrl+Z even on thousands of features.
  • Create new in-memory layer โ€” creates a sorted copy without modifying the original.

Processing Toolbox & PyQGIS

GeoSort is also available in the Processing Toolbox: Toolbox โ†’ GeoSort โ†’ Sort features (GeoSort), usable in batch, in the graphical modeler and via headless PyQGIS:

import processing
result = processing.run("geosort:geosort_sort", {
    'INPUT': layer,
    'CRITERION': 0,          # 0 = Table attribute (see criteria list above)
    'ATTRIBUTE_FIELD': 'region',
    'DIRECTION': True,        # True = Ascending
    'NULLS_LAST': True,
    'NATURAL_SORT': False,    # True = Natural Sort (digits as numbers)
    'SECONDARY_CRITERION': 5,     # 5 = Area (polygons)
    'SECONDARY_DIRECTION': False, # False = Descending
    'ADD_VALUE_FIELD': False,
    'START': 0,                # default 1
    'STEP': 10,                 # default 1 โ†’ 0, 10, 20, 30...
    'ORDER_FIELD': 'rank',     # default 'sort_order'
    'OUTPUT': 'memory:'
})
output_layer = result['OUTPUT']

For the centroid_dist criterion (index 3) you can specify a reference point other than the origin:

result = processing.run("geosort:geosort_sort", {
    'INPUT': layer,
    'CRITERION': 3,
    'REF_POINT': '500000,4649776 [EPSG:32633]',
    'DIRECTION': True,
    'OUTPUT': 'memory:'
})

To sort only the selected features from PyQGIS:

from qgis.core import QgsProcessingFeatureSourceDefinition
result = processing.run("geosort:geosort_sort", {
    'INPUT': QgsProcessingFeatureSourceDefinition(layer.id(), selectedFeaturesOnly=True),
    'CRITERION': 1,
    'OUTPUT': 'memory:'
})

Typical use cases

GoalRecommended criterion
Number the plates of a regular map series, or plan a photogrammetric flightSerpentine
Write a GeoPackage with faster spatial reads, or a "continuous path" atlasHilbert curve
Sort cadastral parcels by size, largest to smallestArea (descending)
Number poles, mileage markers or supports along a road/railway/power linePosition along line
Sort buildings by proximity to a road or the coastlineDistance from line
Report sorted by region and, within each region, by areaMulti-criterion (region โ†’ area)
Sort alphanumeric codes like FILE1, FILE2, FILE10 in "natural" orderAttribute + Natural Sort
Radial ordering from an operations center or network nodeCentroid โ€” distance

FAQ / Troubleshooting

Some features always end up at the bottom โ€” why?

They have a NULL/empty geometry, or a type incompatible with the chosen criterion (e.g. points with an "area" criterion on a mixed-geometry layer). GeoSort doesn't raise an error in these cases: it simply pushes those features to the end of the order.

Why does the area order change when I reproject the layer to another CRS?

On geographic CRS (degrees) planar measurements aren't metrically meaningful. See Geodesic measurement: in Auto mode (default) GeoSort automatically switches to measuring on the ellipsoid when it detects a geographic CRS.

Why aren't Serpentine and Hilbert curve available as a secondary criterion?

Both require computing bands or extent over the entire layer before an order can be assigned โ€” incompatible with the "tie-break" role of a secondary criterion, which only operates within groups already ordered by the primary one. The same applies to criteria based on a reference line.

Can I undo the sort after applying it?

Yes, with Ctrl+Z: writing the sort_order field is wrapped in a single undo command, even on thousands of features.