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.