Fix sort function