[GIS] Postgis query to sort geometries by latitude

orderpostgissql

I am looking for an efficient way to get geometries sorted by their latitude (northmost in case of polygons) when querying a postGis database

Something that could work in an order by clause which possibly works independent of the geometry type (as long as the CRS is lat/lon based)

I think the following would be the ideal writing for it

order by ST_North(geometry)

but I am not aware of such an operator

Best Answer

So far I could think of computing the bounding box and getting its maximum Y value like in:

order by ST_YMax(Box2D(geom))

PS: Actually the conversion to a box is not necessary and to sort from north to south desc works like a charm, hence the final query part is

order by ST_YMax(geom) desc