Skip to content

Commit

Permalink
Sort arrays by resolving references.
Browse files Browse the repository at this point in the history
  • Loading branch information
avian2 committed Oct 22, 2022
1 parent c5ed21e commit ed9db68
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions jsonmerge/strategies.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,19 @@ def merge(self, walk, base, head, schema, **kwargs):

return self._merge(walk, base, head, schema, **kwargs)

def sort_array(self, walk, base, sortByRef):
assert walk.is_type(base, "array")

if sortByRef is None:
return

base.sort(key=lambda item: self._resolve_ref(walk, item, sortByRef))

class Append(ArrayStrategy):
def _merge(self, walk, base, head, schema, sortBy=None, **kwargs):
base.val += head.val

if sortBy != None:
base.val.sort(key = lambda i: i[sortBy])
self.sort_array(walk, base, sortBy)

return base

Expand Down Expand Up @@ -228,8 +235,7 @@ def _merge(self, walk, base, head, schema, idRef="id", ignoreId=None, sortBy=Non
j = matching_j[1]
raise BaseInstanceError("Id '%s' was not unique in base" % (base_key,), base[j])

if sortBy != None:
base.val.sort(key = lambda i: i[sortBy])
self.sort_array(walk, base, sortBy)

return base

Expand Down

0 comments on commit ed9db68

Please sign in to comment.