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