20

I have a shapefile and I want to change the order of the features.

Is that possible?

I want to sort the features in the shapefile because I'm embedding this shapefile in a mobile app. The app will display a list of features, and although I could do the sorting at load time, I'd prefer to have them sorted.

4
  • Please only ask about one or other of QGIS and ArcMap within a single question.
    – PolyGeo
    Aug 18 at 10:36
  • @PolyGeo I don't agree with your comment (and closing the question). The question is about one (very specific) problem not a tool, and I don't want to prejudge which tool is best for solving the issue. During 11 years, there have been valuable answers using different tools. I don't think there is any rule to discuss either ArcMap or QGIS exclusively. I ask you to reopen the question.
    – tato
    Aug 19 at 7:34
  • 1
    GIS SE is a Question & Answer database compilation effort. The answers should reflect a solution in the requested platform, otherwise there's no basis for voting between two completely different answers. Not specifying a platform is, in effect, asking multiple questions, which is forbidden by the One question per Question policy.
    – Vince
    Aug 19 at 12:23
  • @tato If asked today I would expect this question to receive 5 quick close votes for lacking focus. The site has evolved a lot in 11 years and it is today’s voting norms which are applied to all posts so that future posters see as many good examples of how to ask a focused question as possible. Closing does not prevent your question from continuing to receive upvotes if people think it is useful or downvotes if they think the opposite.
    – PolyGeo
    Aug 19 at 13:24

7 Answers 7

17

For QGIS there is a Sort plugin: http://michaelminn.com/linux/mmqgis/

enter image description here

0
13

Here is a solution to rewrite a new and sorted shapefile using the GDAL/OGR command-line tool ogr2ogr.

For example, a shapefile orig.shp has a numeric field volume to sort on. This particular SQL statement does a reverse sort (with DESC) so that features with large volume values are drawn before (underneath) features with small values:

ogr2ogr -sql "SELECT * FROM orig ORDER BY volume DESC" sorted.shp orig.shp
7

You can use the Sort in the (Data Management toolbox) which:

Reorders, in ascending or descending order, records in a feature class or table based on one or multiple fields. The reordered result is written to a new dataset.

0
6

You can always open the attribute table in ArcMap and right click the columns header and select Sort Ascending.../Descending... for single fields.

For multiple fields you would instead click Advanced Sorting to select multiple fields.

NOTE: Adding, for instance, a sequential ID field based on the current sorting (which is temporary, stored only in this map/mxd) when using the above method will be ordered by the original OID/ObjectID of the feature class.

There is an ArcScripts available that will sort records permanently and output a new shapefile: http://arcscripts.esri.com/details.asp?dbid=16771

I do hope that when you state 'order of features' that this is the order found in the attribute table and not drawing order for the table of contents/symbology levels.

0
4

An utility software of MapServer (free and open-source) can sort shapes according to attributes values :

http://mapserver.org/utilities/sortshp.html

2

(In QGIS 3.12.3) Ensure you have the correct layer highlighted in the Layers panel, then search for sort in Processing > Toolbox. The tool is called Vector general > Order by expression. Choose an attribute column (aka. field) or expression to sort by and click Run:

Partial screenshot showing relevant QGIS UI

0
1

A very, very FAST and easy method to sort a shapefile layer( using various fields) .

1 - Export shapefile to CSV (select good separator, like semicolon) and add GEOMETRY using "layer options" -> "AS WKT"

enter image description here

2 - Open file.csv with LIBREOFFICE (calc) and use MENU->DATA->SORT (very intuitive, powerfull and VERY FAST (Sort on the fly 50000 features using 3 columns like sort-key, ascending and descending options, and i have a very slow notebook ;) )

3 - Save again file from LIBREOFFICE (calc) as "Text CSV" ( mark "Edit filter settings" and choose separator as 'semicolon', don't matter warning from libreoffice, save like CSV and select 'semicolon' as separator )

4 - From Qgis open the new file.csv (and sorted) from 'Add Layer" menu -> Add Delimiter Text Layer.

PROS: - Very,very fast, sort from various fields - Works ok with UTF_8 encoded data

CONS: - Need LIBREOFFICE (but is it open source software)

Alternative method to sort (sustitute points 2 and 3, and is the most fast) using console (BASH)

  • Open console and go where you have file.csv

    Suppose you want sort file with the key:

    field6(desc)+field1(ascen)+field3(desce)

    so the command will be:

    sort -t';' -k6,6r -k1,1 -k3,3r file.csv > file_sort.csv

    NOTES:

    • you can add -kn,n for every column number 'n' (field) you needed add to your "sort key"
    • adding 'r' after every n,n will sort in reverse mode (descent)
    • Must pass the character separator used into csv file with -t param
0

Not the answer you're looking for? Browse other questions tagged or ask your own question.