Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

In the Answer to somebody else's question 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

share|improve this question

3 Answers 3

up vote 8 down vote accepted

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.

share|improve this answer
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
    
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

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

share|improve this answer
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

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)
share|improve this answer
    
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 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 Cher Aug 2 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 Cher Aug 2 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 at 8:17

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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