implemented sort function