Skip to content

Commit 87286cb

Browse files
Merge pull request #204 from NREL/rjf/bugfix-sort-filter-ordering
ensure filter occurs before sort
2 parents 8693b79 + b47654c commit 87286cb

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

nrel/hive/util/dict_ops.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,8 @@ def iterate_sim_coll(
9696
) -> Tuple[V, ...]:
9797
"""
9898
helper to iterate through a collection on the SimulationState with optional
99-
sort key function and filter function
99+
sort key function and filter function. performs filter before sort if both
100+
are provided.
100101
101102
:param collection: collection on SimulationState
102103
:type collection: immutables.Map[K, V]
@@ -108,11 +109,12 @@ def iterate_sim_coll(
108109
:rtype: Tuple[V, ...]
109110
"""
110111

111-
vals = DictOps.iterate_vals(collection, sort_key)
112112
if filter_function:
113-
return tuple(filter(filter_function, vals))
113+
entities = immutables.Map({k: v for k, v in collection.items() if filter_function(v)})
114114
else:
115-
return vals
115+
entities = collection
116+
vals = DictOps.iterate_vals(entities, sort_key)
117+
return vals
116118

117119
@classmethod
118120
def add_to_dict(cls, xs: immutables.Map[K, V], obj_id: K, obj: V) -> immutables.Map[K, V]:

0 commit comments

Comments
 (0)