-1

I have been trying to figure out how to change the sorting order for a file geodatabase for ArcGIS. I have been using Python and Arcpy to convert many CAD files into a File Geodatabase Feature Dataset. I succeeded in doing this, but the results are reported in lexicographic order, I understand it correctly. That is it is sorted as such:

1, 10, 12, 2, 20, 200, 3, 33, 34, etc.

While this is is a sensible reading style, I have been looking to get it into a 'natural / human' order. I have done plenty of research on this concept, and found the code that makes this happen.

How to sort alpha numeric set in python

Sorting alphanumeric strings in Python

So now the issue is reordering all of my feature datasets in 'natural order.'

This is the code that allows me to print the datasets in order, but it does not actually edit the geodatabase in mind.

import re, arcpy

arcpy.env.workspace = r"<input geodatabase location>"
datasets = arcpy.ListDatasets()

# Natural sort through input
def sorted_nicely(l):
""" Sort the given iterable in the way that humans expect."""
convert = lambda text: int(text) if text.isdigit() else text
alphanum_key = lambda key: [ convert(c) for c in re.split('([0-9]+)', key) ]
return sorted(l, key = alphanum_key)

# Iterate through listed datasets
for x in sorted_nicely(datasets):
    print(x)

This piece of code, of which the difficult sorting script was used from the above sources, sorts and prints the desired result in my IDE, but it does not actually do anything to my file geodatabase.

I would like to do this without using any modules that are not pre-installed since that would require me to get permissions for further downloads (i.e. further complication).

Thanks!

Community
  • 1
  • 1
  • I don't think you will be able to affect the sort order that is used within ArcMap or ArcCatalog, since that's really just a _display_ sort, not stored within the geodatabase. Are you willing to rename your datasets to include leading zeros? – Erica Mar 23 '17 at 13:26
  • Thanks for getting leaving an answer. I might be able to do that, but I would have to figure out some way to fill in a specific space between the filename with two, or one digits, and exclude certain filenames. I will try to look into it. – heinrich1223 Mar 24 '17 at 14:51

1 Answers1

0

I am not aware of a way to affect sort order -- ArcMap or ArcCatalog use their sort for display purposes only, not by any setting within the geodatabase.

If you are willing to rename the feature datasets, this GIS.SE post has a useful outline of how it works: Batch renaming of feature datasets, and feature classes based on new dataset name? You'd also need to be accounting for variability in length of number to figure out how to add leading zeros, but that could be a new good question to ask :)

Community
  • 1
  • 1
Erica
  • 2,399
  • 5
  • 26
  • 34