Improvements for ordering in custom exports

http://manage.dimagi.com/default.asp?86347#695228
Assign to global. Should say `var sorted = ...`