4

I have a feature class being used in Data Driven Pages, which sorts as follows:

  • APPLE LARRY
  • JOHNSON SHIRLEY
  • KENT-STATE-GOODS LLC
  • KENT TIMOTHY
  • KENT VANESSA
  • KENT WAYNE
  • SMITH JEEVES

I also have the Data Driven Pages run via a script, and because we don't yet have ESRI Production Mapping, table generation has to be done as part of this script. When this script runs, a Python list is created containing these names. My list parcelOwners[] is then sorted using parcelOwners.sort(), but I get a different sort order amongst the KENT's because of the hyphen. That sort order is as follows:

  • APPLE LARRY
  • JOHNSON SHIRLEY
  • KENT TIMOTHY
  • KENT VANESSA
  • KENT WAYNE
  • KENT-STATE-GOODS LLC
  • SMITH JEEVES

As you could imagine, the difference in sort order causes problems when the tables generate for a given set of pages amongst the KENT names. Page 3 gets the table data intended for Page 4, 4 for 5, 5 for 6, and 6 for 3. Is there a way to change how Python sorts my list when it encounters hyphens to match how ArcMap sorts the same data?

5

Use sorted with a lambda function replacing "-" with spaces:

names = ['APPLE LARRY','KENT TIMOTHY','KENT VANESSA','KENT WAYNE','KENT-STATE-GOODS LLC','SMITH JEEVES','JOHNSON SHIRLEY']
sorted(names, key=lambda x: x.replace("-", " "))
print names

>['APPLE LARRY', 'JOHNSON SHIRLEY', 'KENT-STATE-GOODS LLC', 'KENT TIMOTHY', 'KENT VANESSA', 'KENT WAYNE', 'SMITH JEEVES']
  • 1
    Perfect! Thank you so much @BERA! This is exactly what I was looking for. – Zachary Ordo - GISP Jan 25 '18 at 15:46

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.