Optimized sorting (for tables with 100s of rows)

Sorting itself was fast but the for/while loops in sort_map() were slow.
I found it faster to group sort data + TR references into a tuple, sort
the tuple array and then use the TR reference to append the rows back.