sorting and rewriting data tables