Reworked table sort function.