move _type_sort to the module level