14

In the Answer to Reordering fields (permanently) in file geodatabase using ArcGIS Desktop? it was stated that the Make Query Table tool could be used to re-order fields in tables and feature classes permanently.

However, when I open the dialog for that tool I cannot see anywhere that enables re-ordering of fields. All I can see is that fields can be carried through or dropped using checkboxes but in the same order.

Assuming that it is possible can someone describe the steps in more detail, please?

Make Query Table tool dialog

12

I figured out how to do this by using the Make Query Table tool, Copy As Python Snippet, the Python window and the Copy Features tool.

After running the Make Query Table tool to pull through just the fields I wanted to appear in the output I was able to Copy As Python Snippet this code from the Geoprocessing | Results window into the Python window of ArcMap.

arcpy.MakeQueryTable_management("'C:/avhome/data/WAregional/wa regional.mdb/schools'","QueryTable","USE_KEY_FIELDS","#","schools.OBJECTID #;schools.Shape #;schools.CODE #;schools.NAME #;schools.TYPE #;schools.Y11STUDENT #;schools.Y12STUDENT #;schools.COORDGEOCO #;schools.ID #","#")

and edit it to become:

arcpy.MakeQueryTable_management("'C:/avhome/data/WAregional/wa regional.mdb/schools'","QueryTable2","USE_KEY_FIELDS","#","schools.OBJECTID #;schools.Shape #;schools.Y12STUDENT #;schools.Y11STUDENT #;schools.NAME #","#")

Note that the new QueryTable2 retains the Shape field (so I can CopyFeatures it) and I have reordered the NAME,YR11STUDENT & YR12STUDENT fields. I also used the opportunity to drop out a few more fields.

The last step is to use the Copy Features tool on QueryTable2 which I did via its tool dialog to create a new feature class with the fields re-ordered permanently.

  • 2
    Note: you can also use MakeQueryTable to change the names of your fields by inputting a list of lists of field names and aliases ([[fieldname1,alias1],[fieldname2,alias2]...]) – ndimhypervol Dec 25 '12 at 18:02
  • 1
    The same functionality is exposed when you run Feature class to Feature class and then Copy As Python Snippet. – Alex Tereshenkov Apr 5 '14 at 8:28
2

With the Merge Tool, you can easily reorder fields permanently. It works with tables and feature classes. The reordering can be done through python script and even with the Tool dialog (By removing a field and re-adding it in the dialog). Although re-ordering via the dialog is not a perfect approach.

It is recommended to use Merge tool once and then use Copy As Python Snippet and then manually change the fields orders and then paste the python code in python windows.

Here is a python script that uses the Merge Tool to reorder fields (Copied from here)

import arcpy

def reorder_fields(table, out_table, field_order, add_missing=True):
    """ 
    Reorders fields in input featureclass/table
    :table:         input table (fc, table, layer, etc)
    :out_table:     output table (fc, table, layer, etc)
    :field_order:   order of fields (objectid, shape not necessary)
    :add_missing:   add missing fields to end if True (leave out if False)
    -> path to output table
    """
    existing_fields = arcpy.ListFields(table)
    existing_field_names = [field.name for field in existing_fields]

    existing_mapping = arcpy.FieldMappings()
    existing_mapping.addTable(table)

    new_mapping = arcpy.FieldMappings()

    def add_mapping(field_name):
        mapping_index = existing_mapping.findFieldMapIndex(field_name)

        # required fields (OBJECTID, etc) will not be in existing mappings
        # they are added automatically
        if mapping_index != -1:
            field_map = existing_mapping.fieldMappings[mapping_index]
            new_mapping.addFieldMap(field_map)

    # add user fields from field_order
    for field_name in field_order:
        if field_name not in existing_field_names:
            raise Exception("Field: {0} not in {1}".format(field_name, table))

        add_mapping(field_name)

    # add missing fields at end
    if add_missing:
        missing_fields = [f for f in existing_field_names if f not in field_order]
        for field_name in missing_fields:
            add_mapping(field_name)

    # use merge with single input just to use new field_mappings
    arcpy.Merge_management(table, out_table, new_mapping)
    return out_table

USAGE:

new_field_order = ["field2", "field3", "field1"]
reorder_fields(in_fc, out_fc, new_field_order)
  • Would you be able to list the precise steps that you use to do this through its tool dialog, please? From my 10.3.1 test just now it looks like the field mapping panel does not allow re-ordering. Also, I think this answer will be better placed on another question (gis.stackexchange.com/questions/32119/…) because this one is specifically relates to the use of the Make Query Table tool. – PolyGeo Aug 2 '15 at 7:39
  • Yes, I noticed that in your question title. But this post may favor future visitors that only need to reorder fields, the easiest way. – Farid Cheraghi Aug 2 '15 at 7:46
  • The reordering can be done by removing the a field and re-adding it in the dialog. Not a perfect approach though. – Farid Cheraghi Aug 2 '15 at 7:47
  • 1
    I voted for this answer on the other question where I think it is now the best answer but because it's a "duplicate answer", and not a direct answer to the question asked here, I don't think a second vote would be appropriate. – PolyGeo Aug 2 '15 at 8:17
  • @FaridCher, thanks for sharing the code. Just needed to re-order fields really quickly in Python, and it worked great. Awesome. – Alex Tereshenkov Feb 25 '16 at 17:02
1

Having read this Altering the order of attributes in a table and trying to rearrange the order of fields, I found this simple solution in ArcMap 10,1...

  1. Right-click on feature you want to alter in table of contents
  2. click Properties
  3. click Fields tab
  4. click on a field
  5. click the "move up" or "move down" arrow
  6. Apply, OK

enter image description here

  • 3
    Good try. But unfortunately this applies only to the layer you've added into the map document. The feature class will still have the fields in the order defined. Technically speaking, there is no "order" of fields in databases, but as an end user you might want to be able when adding a feature class into a map document and opening an attribute table see the fields in the relevant for you order. – Alex Tereshenkov Apr 24 '14 at 14:20
1

I have a method of doing it completely within model builder in a relatively robust manner. It is a little tedious to set up but at least it can be inserted as part of a larger model-builder analysis:

Dissolve tool - ObjectID as the dissolve field. Add the remaining (Statistics) fields in the desired order and chose the option (First) out of the Statstic Type drop down. In the example below I have moved the field SaltMarsh_Pct up form way down in the list of fields to position 6.

Dissolve Model with reordered fields

I generally copy the results then rename each field (the copy is functionally redundant and the rename can be done there as well but it is flaky)

Use the Alter Field tool to rename each field

Alter Field names back to original (removing FIRST)

All together in a model it looks like this:

Example model to reorder

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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