Backport large sort fixes

Backports gh-2974