Fix recfunctions.join_by bugs (issue  #4216)

This fixes the issue: "BUG: recfunctions.join_by fails if the keys are not in the same order #4216" and another bug.