Begin sort function.