wrote some functions for sorting