added query version of table sorting