Moving sort function