sorter function to seperate and sort input