Skip to main content
Solved

How do I sort a list of features by an attribute value in a PythonCaller?

  • October 31, 2023
  • 1 reply
  • 173 views

dms2
Contributor
Forum|alt.badge.img+11

I'm working with groups of features and I want minimize the number of blocking transformers in my workflow. Right now I'm using a Sorter to sort features in each group and a PythonCaller to make some attribute manipulation and comparison. Both transformers block the flow so  I wanted to get rid of the Sorter and do the feature sorting inside the PythonCaller but I don't see how to do it.

Once I have the list of features, what should I do to sort them by one or any number of attributes?

 

Thank you!

 

import fme
import fmeobjects
 
class FeatureProcessor(object):
 
    def __init__(self):
        self.feature_list = []
 
    def input(self, feature):
        self.feature_list.append(feature)
 
    def close(self):
        pass
 
    def process_group(self):
        for feature in self.feature_list:
             ## ...

 

 

Best answer by david_r

Have a look at the tutorial here: https://www.geeksforgeeks.org/sort-a-list-of-objects-by-multiple-attributes-in-python/

You'll want to skip the "for" loop on line 16 and pass self.feature_list directly to the sort() function.

This post is closed to further activity.
It may be an old question, an answered question, an implemented idea, or a notification-only post.
Please check post dates before relying on any information in a question or answer.
For follow-up or related questions, please post a new question or idea.
If there is a genuine update to be made, please contact us and request that the post is reopened.

1 reply

david_r
Celebrity
  • Best Answer
  • October 31, 2023

Have a look at the tutorial here: https://www.geeksforgeeks.org/sort-a-list-of-objects-by-multiple-attributes-in-python/

You'll want to skip the "for" loop on line 16 and pass self.feature_list directly to the sort() function.