Function sortLengthFirstDeterministic