Improvements for ordering in custom exports

http://manage.dimagi.com/default.asp?86347#695228
Since `tag_order` is effectively a constant, it should be defined outside the sort function as it was before.