Improvements for ordering in custom exports

Reverts dimagi/commcare-hq#4205

Picking this up again since https://github.com/dimagi/commcare-hq/pull/3841 was mistakenly merged.