fix many bugs of the process of sorting the elements and rows