Allow partial list of ids for sortByIds

Refactored sortByIds to be more robust and predictable in the case of an incomplete list of ids to use for sorting. Any unlisted ids will be appended to the end in their original order.
What is the difference between the test `(1)` and `(2)`? Please use a more descriptive name that will highlight this difference. Perhaps `(1)` is redundant as the same functionality/code is already covered by `(2)`?