implemented sort instead of if statements to arrange lengths