proper table sorting algorithm