fixed sort function