create sort function