refactored the sort method for sorted sorted occerrences and lengths