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?

1 Answer 1

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
  • 1
    Perfect! Thank you so much @BERA! This is exactly what I was looking for. Jan 25, 2018 at 15:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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