refactor sorting and get_data