Mike refactor table sort

Suggest you review the whole file alone instead of diff