improve table sort function