Allow fields to be sorted by orderby …

Two separate but connected issues.  First if orderby is passed then the $data array is
never populated.  Then looping through $ids will always give it results sorted by priority
in the relationships field (data returned by lookup_related_items)

Fixes #2277
Add missed brace
Merge pull request #2350 from jamesgol/orderbyfield

Allow fields to be sorted by orderby & fix #2277